signature: disallow encoding invalid SignatureInfo
Also, check integer range when decoding SignatureInfo.
refs #3200
Change-Id: I1af4833211c9468ac7ecab48f7d0e88e9423b378
diff --git a/src/signature-info.cpp b/src/signature-info.cpp
index 36964ac..0b3b0a1 100644
--- a/src/signature-info.cpp
+++ b/src/signature-info.cpp
@@ -60,6 +60,10 @@
size_t
SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const
{
+ if (m_type == -1) {
+ BOOST_THROW_EXCEPTION(Error("Cannot encode invalid SignatureInfo"));
+ }
+
// SignatureInfo ::= SIGNATURE-INFO-TLV TLV-LENGTH
// SignatureType
// KeyLocator?
@@ -114,15 +118,15 @@
Block::element_const_iterator it = m_wire.elements_begin();
- // the first block must be SignatureType
+ // the first sub-element must be SignatureType
if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) {
- m_type = readNonNegativeInteger(*it);
+ m_type = readNonNegativeIntegerAs<int32_t>(*it);
++it;
}
else
BOOST_THROW_EXCEPTION(Error("Missing SignatureType in SignatureInfo"));
- // the second block could be KeyLocator
+ // the second sub-element could be KeyLocator
if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) {
m_keyLocator.wireDecode(*it);
m_hasKeyLocator = true;
diff --git a/src/signature.cpp b/src/signature.cpp
index 3683449..4ac9d59 100644
--- a/src/signature.cpp
+++ b/src/signature.cpp
@@ -40,6 +40,15 @@
{
}
+tlv::SignatureTypeValue
+Signature::getType() const
+{
+ if (!*this) {
+ BOOST_THROW_EXCEPTION(Error("Signature is invalid"));
+ }
+ return static_cast<tlv::SignatureTypeValue>(m_info.getSignatureType());
+}
+
void
Signature::setInfo(const Block& info)
{
@@ -55,4 +64,10 @@
m_value = value;
}
+bool
+operator==(const Signature& lhs, const Signature& rhs)
+{
+ return lhs.getSignatureInfo() == rhs.getSignatureInfo() && lhs.getValue() == rhs.getValue();
+}
+
} // namespace ndn
diff --git a/src/signature.hpp b/src/signature.hpp
index 315d061..1af7885 100644
--- a/src/signature.hpp
+++ b/src/signature.hpp
@@ -109,12 +109,10 @@
public: // SignatureInfo fields
/** @brief Get SignatureType
+ * @throw Error signature is invalid
*/
- uint32_t
- getType() const
- {
- return m_info.getSignatureType();
- }
+ tlv::SignatureTypeValue
+ getType() const;
/** @brief Check if KeyLocator exists in SignatureInfo
*/
@@ -157,11 +155,8 @@
mutable Block m_value;
};
-inline bool
-operator==(const Signature& lhs, const Signature& rhs)
-{
- return lhs.getInfo() == rhs.getInfo() && lhs.getValue() == rhs.getValue();
-}
+bool
+operator==(const Signature& lhs, const Signature& rhs);
inline bool
operator!=(const Signature& lhs, const Signature& rhs)