security: Add AdditionalInfo into SignatureInfo
Change-Id: I5e5c85b09b7a00ef4182bec3267e444f33a706ff
Refs: #3058
diff --git a/src/encoding/tlv.hpp b/src/encoding/tlv.hpp
index 1c4851a..7d7461f 100644
--- a/src/encoding/tlv.hpp
+++ b/src/encoding/tlv.hpp
@@ -106,7 +106,12 @@
// SignatureInfo TLVs
ValidityPeriod = 253,
NotBefore = 254,
- NotAfter = 255
+ NotAfter = 255,
+
+ AdditionalDescription = 258,
+ DescriptionEntry = 512,
+ DescriptionKey = 513,
+ DescriptionValue = 514
};
/** @brief indicates a possible value of ContentType field
diff --git a/src/security/additional-description.cpp b/src/security/additional-description.cpp
new file mode 100644
index 0000000..6aed34a
--- /dev/null
+++ b/src/security/additional-description.cpp
@@ -0,0 +1,197 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "additional-description.hpp"
+#include "../util/concepts.hpp"
+#include "../encoding/block-helpers.hpp"
+
+namespace ndn {
+namespace security {
+
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>));
+BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>));
+BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<AdditionalDescription>));
+BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>));
+static_assert(std::is_base_of<tlv::Error, AdditionalDescription::Error>::value,
+ "AdditionalDescription::Error must inherit from tlv::Error");
+
+static const size_t KEY_OFFSET = 0;
+static const size_t VALUE_OFFSET = 1;
+
+AdditionalDescription::AdditionalDescription(const Block& block)
+{
+ wireDecode(block);
+}
+
+const std::string&
+AdditionalDescription::get(const std::string& key) const
+{
+ auto it = m_info.find(key);
+ if (it == m_info.end())
+ throw Error("Entry does not exist for key (" + key + ")");
+
+ return it->second;
+}
+
+void
+AdditionalDescription::set(const std::string& key, const std::string& value)
+{
+ m_info[key] = value;
+}
+
+bool
+AdditionalDescription::has(const std::string& key) const
+{
+ return (m_info.find(key) != m_info.end());
+}
+
+AdditionalDescription::iterator
+AdditionalDescription::begin()
+{
+ return m_info.begin();
+}
+
+AdditionalDescription::iterator
+AdditionalDescription::end()
+{
+ return m_info.end();
+}
+
+AdditionalDescription::const_iterator
+AdditionalDescription::begin() const
+{
+ return m_info.begin();
+}
+
+AdditionalDescription::const_iterator
+AdditionalDescription::end() const
+{
+ return m_info.end();
+}
+
+template<encoding::Tag TAG>
+size_t
+AdditionalDescription::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+ size_t totalLength = 0;
+
+ for (auto it = m_info.rbegin(); it != m_info.rend(); it++) {
+ size_t entryLength = 0;
+ entryLength += prependStringBlock(encoder, tlv::DescriptionValue, it->second);
+ entryLength += prependStringBlock(encoder, tlv::DescriptionKey, it->first);
+ entryLength += encoder.prependVarNumber(entryLength);
+ entryLength += encoder.prependVarNumber(tlv::DescriptionEntry);
+
+ totalLength += entryLength;
+ }
+
+ totalLength += encoder.prependVarNumber(totalLength);
+ totalLength += encoder.prependVarNumber(tlv::AdditionalDescription);
+ return totalLength;
+}
+
+template size_t
+AdditionalDescription::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+AdditionalDescription::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+AdditionalDescription::wireEncode() const
+{
+ if (m_wire.hasWire())
+ return m_wire;
+
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ m_wire = buffer.block();
+ m_wire.parse();
+
+ return m_wire;
+}
+
+void
+AdditionalDescription::wireDecode(const Block& wire)
+{
+ if (!wire.hasWire()) {
+ throw Error("The supplied block does not contain wire format");
+ }
+
+ m_wire = wire;
+ m_wire.parse();
+
+ if (m_wire.type() != tlv::AdditionalDescription)
+ throw Error("Unexpected TLV type when decoding AdditionalDescription");
+
+ Block::element_const_iterator it = m_wire.elements_begin();
+ while (it != m_wire.elements_end()) {
+ const Block& entry = *it;
+ entry.parse();
+
+ if (entry.type() != tlv::DescriptionEntry)
+ throw Error("Unexpected TLV type when decoding DescriptionEntry");
+
+ if (entry.elements_size() != 2)
+ throw Error("DescriptionEntry does not have two sub-TLVs");
+
+ if (entry.elements()[KEY_OFFSET].type() != tlv::DescriptionKey ||
+ entry.elements()[VALUE_OFFSET].type() != tlv::DescriptionValue)
+ throw Error("Invalid DescriptionKey or DescriptionValue field");
+
+ m_info[readString(entry.elements()[KEY_OFFSET])] = readString(entry.elements()[VALUE_OFFSET]);
+ it++;
+ }
+}
+
+bool
+AdditionalDescription::operator==(const AdditionalDescription& other) const
+{
+ return (m_info == other.m_info);
+}
+
+bool
+AdditionalDescription::operator!=(const AdditionalDescription& other) const
+{
+ return !(*this == other);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const AdditionalDescription& other)
+{
+ size_t count = 0;
+ os << "(";
+ for (const auto& entry : other) {
+ if (count > 0)
+ os << ", ";
+ os << "(" << entry.first << ":" << entry.second << ")";
+ count++;
+ }
+ os << ")";
+
+ return os;
+}
+
+} // namespace security
+} // namespace ndn
diff --git a/src/security/additional-description.hpp b/src/security/additional-description.hpp
new file mode 100644
index 0000000..b34cb74
--- /dev/null
+++ b/src/security/additional-description.hpp
@@ -0,0 +1,131 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_SECURITY_ADDITIONAL_DESCRIPTION_HPP
+#define NDN_SECURITY_ADDITIONAL_DESCRIPTION_HPP
+
+#include "../common.hpp"
+#include "../encoding/tlv.hpp"
+#include "../encoding/block.hpp"
+#include <map>
+
+namespace ndn {
+namespace security {
+
+/**
+ * @brief Abstraction of AdditionalDescription
+ * @sa docs/tutorials/certificate-format.rst
+ */
+class AdditionalDescription
+{
+public:
+ class Error : public tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : tlv::Error(what)
+ {
+ }
+ };
+
+ typedef std::map<std::string, std::string>::iterator iterator;
+ typedef std::map<std::string, std::string>::const_iterator const_iterator;
+
+public:
+
+ /**
+ * @brief Create an empty AdditionalDescription
+ */
+ AdditionalDescription() = default;
+
+ /**
+ * @brief Create AdditionalDescription from @p block
+ */
+ explicit
+ AdditionalDescription(const Block& block);
+
+ const std::string&
+ get(const std::string& key) const;
+
+ void
+ set(const std::string& key, const std::string& value);
+
+ bool
+ has(const std::string& key) const;
+
+ size_t
+ size() const
+ {
+ return m_info.size();
+ }
+
+ iterator
+ begin();
+
+ iterator
+ end();
+
+ const_iterator
+ begin() const;
+
+ const_iterator
+ end() const;
+
+ /** @brief Fast encoding or block size estimation
+ */
+ template<encoding::Tag TAG>
+ size_t
+ wireEncode(EncodingImpl<TAG>& encoder) const;
+
+ /** @brief Encode ValidityPeriod into TLV block
+ */
+ const Block&
+ wireEncode() const;
+
+ /** @brief Decode ValidityPeriod from TLV block
+ * @throw Error when an invalid TLV block supplied
+ */
+ void
+ wireDecode(const Block& wire);
+
+public: // EqualityComparable concept
+
+ bool
+ operator==(const AdditionalDescription& other) const;
+
+ bool
+ operator!=(const AdditionalDescription& other) const;
+
+private:
+
+ std::map<std::string, std::string> m_info;
+
+ mutable Block m_wire;
+};
+
+std::ostream&
+operator<<(std::ostream& os, const AdditionalDescription& period);
+
+} // namespace security
+} // namespace ndn
+
+#endif // NDN_SECURITY_ADDITIONAL_DESCRIPTION_HPP
diff --git a/tests/unit-tests/security/additional-info.t.cpp b/tests/unit-tests/security/additional-info.t.cpp
new file mode 100644
index 0000000..63320f7
--- /dev/null
+++ b/tests/unit-tests/security/additional-info.t.cpp
@@ -0,0 +1,101 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "security/additional-description.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(SecurityAdditionalDescription)
+
+static const uint8_t description[] = {
+ 0xfd, 0x01, 0x02, 0x28,
+ 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry
+ 0xfd, 0x02, 0x01, 0x04, // DescriptionKey
+ 0x6b, 0x65, 0x79, 0x31, // "key1"
+ 0xfd, 0x02, 0x02, 0x04, // DescriptionValue
+ 0x76, 0x61, 0x6c, 0x31, // "val1"
+ 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry
+ 0xfd, 0x02, 0x01, 0x04, // DescriptionKey
+ 0x6b, 0x65, 0x79, 0x32, // "key2"
+ 0xfd, 0x02, 0x02, 0x04, // DescriptionValue
+ 0x76, 0x61, 0x6c, 0x32, // "val2"
+};
+
+static const std::string text = "((key1:val1), (key2:val2))";
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ AdditionalDescription aDescription;
+
+ aDescription.set("key2", "val2");
+ aDescription.set("key1", "val1");
+
+ BOOST_REQUIRE_NO_THROW(aDescription.get("key1"));
+ BOOST_REQUIRE_NO_THROW(aDescription.get("key2"));
+ BOOST_REQUIRE_THROW(aDescription.get("key3"), AdditionalDescription::Error);
+
+ BOOST_CHECK_EQUAL(aDescription.has("key1"), true);
+ BOOST_CHECK_EQUAL(aDescription.has("key2"), true);
+ BOOST_CHECK_EQUAL(aDescription.has("key3"), false);
+
+ auto val1 = aDescription.get("key1");
+ auto val2 = aDescription.get("key2");
+
+ BOOST_CHECK_EQUAL(val1, "val1");
+ BOOST_CHECK_EQUAL(val2, "val2");
+
+ auto it = aDescription.begin();
+ BOOST_CHECK_EQUAL(it->second, "val1");
+ it++;
+ BOOST_CHECK_EQUAL(it->second, "val2");
+ it++;
+ BOOST_CHECK(it == aDescription.end());
+
+ BOOST_CHECK_EQUAL_COLLECTIONS(aDescription.wireEncode().wire(),
+ aDescription.wireEncode().wire() + aDescription.wireEncode().size(),
+ description,
+ description + sizeof(description));
+
+ BOOST_REQUIRE_NO_THROW(AdditionalDescription(Block(description, sizeof(description))));
+ AdditionalDescription aDescription2(Block(description, sizeof(description)));
+
+ BOOST_CHECK(aDescription2 == aDescription);
+
+ AdditionalDescription aDescription3;
+ aDescription3.set("key3", "val3");
+ aDescription3.set("key2", "val2");
+
+ BOOST_CHECK(aDescription2 != aDescription3);
+
+ std::ostringstream os;
+ os << aDescription;
+ BOOST_CHECK_EQUAL(os.str(), text);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace security
+} // namespace ndn