Example #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));
        }
Example #2
0
        public HeartbeatMessage(byte type, byte[] payload, int paddingLength)
        {
            if (!HeartbeatMessageType.IsValid(type))
            {
                throw new ArgumentException("not a valid HeartbeatMessageType value", "type");
            }
            if (payload == null || payload.Length >= (1 << 16))
            {
                throw new ArgumentException("must have length < 2^16", "payload");
            }
            if (paddingLength < 16)
            {
                throw new ArgumentException("must be at least 16", "paddingLength");
            }

            this.mType          = type;
            this.mPayload       = payload;
            this.mPaddingLength = paddingLength;
        }