Encrypted content
Change-Id: Iabe169a07abd2237a3573b51efc0ad6ae31c85e2
diff --git a/src/encrypted-content.cpp b/src/encrypted-content.cpp
new file mode 100644
index 0000000..a2ca2a4
--- /dev/null
+++ b/src/encrypted-content.cpp
@@ -0,0 +1,230 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018, Regents of the University of California
+ *
+ * NAC 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.
+ *
+ * NAC 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 NAC library authors and contributors.
+ */
+
+#include "encrypted-content.hpp"
+
+#include <ndn-cxx/encoding/block-helpers.hpp>
+#include <ndn-cxx/util/concepts.hpp>
+
+namespace ndn {
+namespace nac {
+
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<EncryptedContent>));
+BOOST_CONCEPT_ASSERT((WireEncodable<EncryptedContent>));
+BOOST_CONCEPT_ASSERT((WireDecodable<EncryptedContent>));
+static_assert(std::is_base_of<ndn::tlv::Error, EncryptedContent::Error>::value,
+ "EncryptedContent::Error must inherit from tlv::Error");
+
+EncryptedContent::EncryptedContent(const Block& block)
+{
+ wireDecode(block);
+}
+
+EncryptedContent&
+EncryptedContent::setPayload(Block payload)
+{
+ m_wire.reset();
+ if (payload.type() != tlv::EncryptedPayload) {
+ m_payload = Block(tlv::EncryptedPayload, std::move(payload));
+ }
+ else {
+ m_payload = std::move(payload);
+ }
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setPayload(ConstBufferPtr payload)
+{
+ m_wire.reset();
+ m_payload = Block(tlv::EncryptedPayload, std::move(payload));
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setIv(Block iv)
+{
+ m_wire.reset();
+ if (iv.type() != tlv::InitialVector) {
+ m_iv = Block(tlv::InitialVector, std::move(iv));
+ }
+ else {
+ m_iv = std::move(iv);
+ }
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setIv(ConstBufferPtr iv)
+{
+ m_wire.reset();
+ m_iv = Block(tlv::InitialVector, iv);
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::unsetIv()
+{
+ m_wire.reset();
+ m_iv = Block();
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setPayloadKey(Block key)
+{
+ m_wire.reset();
+ if (key.type() != tlv::EncryptedPayloadKey) {
+ m_payloadKey = Block(tlv::EncryptedPayloadKey, std::move(key));
+ }
+ else {
+ m_payloadKey = std::move(key);
+ }
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setPayloadKey(ConstBufferPtr key)
+{
+ m_wire.reset();
+ m_payloadKey = Block(tlv::EncryptedPayloadKey, std::move(key));
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::unsetPayloadKey()
+{
+ m_wire.reset();
+ m_payloadKey = Block();
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::setKeyLocator(Name keyLocator)
+{
+ m_wire.reset();
+ m_keyLocator = std::move(keyLocator);
+ return *this;
+}
+
+EncryptedContent&
+EncryptedContent::unsetKeyLocator()
+{
+ m_wire.reset();
+ m_keyLocator = Name();
+ return *this;
+}
+
+template<encoding::Tag TAG>
+size_t
+EncryptedContent::wireEncode(EncodingImpl<TAG>& block) const
+{
+ size_t totalLength = 0;
+
+ if (!m_keyLocator.empty()) {
+ totalLength += m_keyLocator.wireEncode(block);
+ }
+
+ if (!m_payloadKey.empty()) {
+ totalLength += block.prependBlock(m_payloadKey);
+ }
+
+ if (!m_iv.empty()) {
+ totalLength += block.prependBlock(m_iv);
+ }
+
+ if (!m_payload.empty()) {
+ totalLength += block.prependBlock(m_payload);
+ }
+ else {
+ BOOST_THROW_EXCEPTION(Error("Required EncryptedPayload is not set on EncryptedContent"));
+ }
+
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(tlv::EncryptedContent);
+ return totalLength;
+}
+
+const Block&
+EncryptedContent::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();
+ return m_wire;
+}
+
+void
+EncryptedContent::wireDecode(const Block& wire)
+{
+ if (!wire.hasWire()) {
+ BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
+ }
+
+ m_payload.reset();
+ m_iv.reset();
+ m_payloadKey.reset();
+
+ m_wire = wire;
+ m_wire.parse();
+
+ if (m_wire.type() != tlv::EncryptedContent) {
+ BOOST_THROW_EXCEPTION(Error("Unexpected TLV type (expecting EncryptedContent, got" +
+ ndn::to_string(m_wire.type()) + ")"));
+ }
+
+ auto block = m_wire.find(tlv::EncryptedPayload);
+ if (block != m_wire.elements_end()) {
+ m_payload = *block;
+ }
+ else {
+ BOOST_THROW_EXCEPTION(Error("Required EncryptedPayload not found in EncryptedContent"));
+ }
+
+ block = m_wire.find(tlv::InitialVector);
+ if (block != m_wire.elements_end()) {
+ m_iv = *block;
+ }
+
+ block = m_wire.find(tlv::EncryptedPayloadKey);
+ if (block != m_wire.elements_end()) {
+ m_payloadKey = *block;
+ }
+
+ block = m_wire.find(tlv::Name);
+ if (block != m_wire.elements_end()) {
+ m_keyLocator.wireDecode(*block);
+ }
+}
+
+bool
+EncryptedContent::operator==(const EncryptedContent& rhs) const
+{
+ return (wireEncode() == rhs.wireEncode());
+}
+
+} // namespace nac
+} // namespace ndn