aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-11-07 18:48:31 +0000
committerlloyd <[email protected]>2012-11-07 18:48:31 +0000
commit67bd095ee02d059fc64f5227b99b1ec85accc8eb (patch)
tree26e4a335f8e4cf0ca3fadf17b398cc6b8cc521c3 /src/tls
parentf4cd23a0bf6cf298087c7c6086d57b5de980e049 (diff)
Store the maximum fragment value instead of the code, convert to the
code on serialize/deserialize.
Diffstat (limited to 'src/tls')
-rw-r--r--src/tls/tls_extensions.cpp54
-rw-r--r--src/tls/tls_extensions.h14
2 files changed, 31 insertions, 37 deletions
diff --git a/src/tls/tls_extensions.cpp b/src/tls/tls_extensions.cpp
index b4bc066c0..f3c0fc000 100644
--- a/src/tls/tls_extensions.cpp
+++ b/src/tls/tls_extensions.cpp
@@ -204,38 +204,21 @@ std::vector<byte> Renegotiation_Extension::serialize() const
return buf;
}
-size_t Maximum_Fragment_Length::fragment_size() const
+std::vector<byte> Maximum_Fragment_Length::serialize() const
{
- switch(val)
- {
- case 1:
- return 512;
- case 2:
- return 1024;
- case 3:
- return 2048;
- case 4:
- return 4096;
- default:
- throw TLS_Exception(Alert::ILLEGAL_PARAMETER,
- "Bad value in maximum fragment extension");
- }
- }
+ const std::map<size_t, byte> fragment_to_code = { { 512, 1 },
+ { 1024, 2 },
+ { 2048, 3 },
+ { 4096, 4 } };
-Maximum_Fragment_Length::Maximum_Fragment_Length(size_t max_fragment)
- {
- if(max_fragment == 512)
- val = 1;
- else if(max_fragment == 1024)
- val = 2;
- else if(max_fragment == 2048)
- val = 3;
- else if(max_fragment == 4096)
- val = 4;
- else
+ auto i = fragment_to_code.find(m_max_fragment);
+
+ if(i == fragment_to_code.end())
throw std::invalid_argument("Bad setting " +
- std::to_string(max_fragment) +
+ std::to_string(m_max_fragment) +
" for maximum fragment size");
+
+ return std::vector<byte>(1, i->second);
}
Maximum_Fragment_Length::Maximum_Fragment_Length(TLS_Data_Reader& reader,
@@ -243,7 +226,20 @@ Maximum_Fragment_Length::Maximum_Fragment_Length(TLS_Data_Reader& reader,
{
if(extension_size != 1)
throw Decoding_Error("Bad size for maximum fragment extension");
- val = reader.get_byte();
+ byte val = reader.get_byte();
+
+ const std::map<byte, size_t> code_to_fragment = { { 1, 512 },
+ { 2, 1024 },
+ { 3, 2048 },
+ { 4, 4096 } };
+
+ auto i = code_to_fragment.find(val);
+
+ if(i == code_to_fragment.end())
+ throw TLS_Exception(Alert::ILLEGAL_PARAMETER,
+ "Bad value in maximum fragment extension");
+
+ m_max_fragment = i->second;
}
Next_Protocol_Notification::Next_Protocol_Notification(TLS_Data_Reader& reader,
diff --git a/src/tls/tls_extensions.h b/src/tls/tls_extensions.h
index 541bd4966..de3654fd3 100644
--- a/src/tls/tls_extensions.h
+++ b/src/tls/tls_extensions.h
@@ -158,27 +158,25 @@ class Maximum_Fragment_Length : public Extension
Handshake_Extension_Type type() const { return static_type(); }
- bool empty() const { return val != 0; }
+ bool empty() const { return false; }
- size_t fragment_size() const;
+ size_t fragment_size() const { return m_max_fragment; }
- std::vector<byte> serialize() const
- {
- return std::vector<byte>(1, val);
- }
+ std::vector<byte> serialize() const;
/**
* @param max_fragment specifies what maximum fragment size to
* advertise. Currently must be one of 512, 1024, 2048, or
* 4096.
*/
- Maximum_Fragment_Length(size_t max_fragment);
+ Maximum_Fragment_Length(size_t max_fragment) :
+ m_max_fragment(max_fragment) {}
Maximum_Fragment_Length(TLS_Data_Reader& reader,
u16bit extension_size);
private:
- byte val;
+ size_t m_max_fragment;
};
/**