Ejemplo n.º 1
0
        /**
         * Parse a {@link HeartbeatMessage} from a {@link Stream}.
         *
         * @param input
         *            the {@link Stream} to parse from.
         * @return a {@link HeartbeatMessage} object.
         * @throws IOException
         */
        public static HeartbeatMessage Parse(Stream input)
        {
            byte type = TlsUtilities.ReadUint8(input);

            if (!HeartbeatMessageType.IsValid(type))
            {
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);
            }

            int payload_length = TlsUtilities.ReadUint16(input);

            PayloadBuffer buf = new PayloadBuffer();

            Streams.PipeAll(input, buf);

            byte[] payload = buf.ToTruncatedByteArray(payload_length);
            if (payload == null)
            {
                /*
                 * RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the
                 * received HeartbeatMessage MUST be discarded silently.
                 */
                return(null);
            }

            TlsUtilities.CheckUint16(buf.Length);
            int padding_length = (int)buf.Length - payload.Length;

            /*
             * RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
             */
            return(new HeartbeatMessage(type, payload, padding_length));
        }
Ejemplo n.º 2
0
        /**
         * Parse a {@link HeartbeatMessage} from a {@link Stream}.
         * 
         * @param input
         *            the {@link Stream} to parse from.
         * @return a {@link HeartbeatMessage} object.
         * @throws IOException
         */
        public static HeartbeatMessage Parse(Stream input)
        {
            byte type = TlsUtilities.ReadUint8(input);
            if (!HeartbeatMessageType.IsValid(type))
                throw new TlsFatalAlert(AlertDescription.illegal_parameter);

            int payload_length = TlsUtilities.ReadUint16(input);

            PayloadBuffer buf = new PayloadBuffer();
            Streams.PipeAll(input, buf);

            byte[] payload = buf.ToTruncatedByteArray(payload_length);
            if (payload == null)
            {
                /*
                 * RFC 6520 4. If the payload_length of a received HeartbeatMessage is too large, the
                 * received HeartbeatMessage MUST be discarded silently.
                 */
                return null;
            }

            TlsUtilities.CheckUint16(buf.Length);
            int padding_length = (int)buf.Length - payload.Length;

            /*
             * RFC 6520 4. The padding of a received HeartbeatMessage message MUST be ignored
             */
            return new HeartbeatMessage(type, payload, padding_length);
        }
    public static HeartbeatMessage Parse(Stream input)
    {
        byte b = TlsUtilities.ReadUint8(input);

        if (!HeartbeatMessageType.IsValid(b))
        {
            throw new TlsFatalAlert(47);
        }
        int           payloadLength = TlsUtilities.ReadUint16(input);
        PayloadBuffer payloadBuffer = new PayloadBuffer();

        Streams.PipeAll(input, payloadBuffer);
        byte[] array = payloadBuffer.ToTruncatedByteArray(payloadLength);
        if (array == null)
        {
            return(null);
        }
        TlsUtilities.CheckUint16(payloadBuffer.Length);
        int paddingLength = (int)payloadBuffer.Length - array.Length;

        return(new HeartbeatMessage(b, array, paddingLength));
    }