/* * TLS Heartbeats * (C) 2012 Jack Lloyd * * Released under the terms of the Botan license */ #include #include #include #include namespace Botan { namespace TLS { Heartbeat_Message::Heartbeat_Message(const MemoryRegion& buf) { TLS_Data_Reader reader(buf); const byte type = reader.get_byte(); if(type != 1 && type != 2) throw TLS_Exception(Alert::ILLEGAL_PARAMETER, "Unknown heartbeat message type"); m_type = static_cast(type); m_payload = reader.get_range(2, 0, 16*1024); // padding follows and is ignored } Heartbeat_Message::Heartbeat_Message(Type type, const byte payload[], size_t payload_len) : m_type(type), m_payload(payload, payload_len) { } MemoryVector Heartbeat_Message::contents() const { MemoryVector send_buf(3 + m_payload.size() + 16); send_buf[0] = m_type; send_buf[1] = get_byte(0, m_payload.size()); send_buf[2] = get_byte(1, m_payload.size()); copy_mem(&send_buf[3], &m_payload[0], m_payload.size()); // leave padding as all zeros return send_buf; } MemoryVector Heartbeat_Support_Indicator::serialize() const { MemoryVector heartbeat(1); heartbeat[0] = (m_peer_allowed_to_send ? 1 : 2); return heartbeat; } Heartbeat_Support_Indicator::Heartbeat_Support_Indicator(TLS_Data_Reader& reader, u16bit extension_size) { if(extension_size != 1) throw Decoding_Error("Strange size for heartbeat extension"); const byte code = reader.get_byte(); if(code != 1 && code != 2) throw TLS_Exception(Alert::ILLEGAL_PARAMETER, "Unknown heartbeat code " + to_string(code)); m_peer_allowed_to_send = (code == 1); } } }