security: Add SafeBag
Change-Id: Iab7cdbdf391c4feedda731d81b75f028fedd366d
Refs: #3048
diff --git a/src/encoding/tlv-security.hpp b/src/encoding/tlv-security.hpp
index 7d17e1b..ac8f248 100644
--- a/src/encoding/tlv-security.hpp
+++ b/src/encoding/tlv-security.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -34,6 +34,11 @@
CertificatePackage = 130
};
+enum {
+ SafeBag = 128,
+ EncryptedKeyBag = 129
+};
+
} // namespace security
} // namespace tlv
} // namespace ndn
diff --git a/src/security/safe-bag.cpp b/src/security/safe-bag.cpp
new file mode 100644
index 0000000..448847a
--- /dev/null
+++ b/src/security/safe-bag.cpp
@@ -0,0 +1,129 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 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.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+#include "safe-bag.hpp"
+#include "encoding/tlv-security.hpp"
+#include "util/concepts.hpp"
+
+namespace ndn {
+namespace security {
+
+BOOST_CONCEPT_ASSERT((WireEncodable<SafeBag>));
+BOOST_CONCEPT_ASSERT((WireDecodable<SafeBag>));
+
+SafeBag::SafeBag() = default;
+
+SafeBag::SafeBag(const Block& wire)
+{
+ this->wireDecode(wire);
+}
+
+SafeBag::SafeBag(const Data& certificate,
+ const Buffer& encryptedKeyBag)
+ : m_certificate(certificate)
+ , m_encryptedKeyBag(encryptedKeyBag)
+{
+}
+
+SafeBag::SafeBag(const Data& certificate,
+ const uint8_t* encryptedKey,
+ size_t encryptedKeyLen)
+ : m_certificate(certificate)
+ , m_encryptedKeyBag(encryptedKey, encryptedKeyLen)
+{
+}
+
+///////////////////////////////////////////////////// encode & decode
+
+template<encoding::Tag TAG>
+size_t
+SafeBag::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+ size_t totalLength = 0;
+
+ // EncryptedKeyBag
+ totalLength += encoder.prependByteArrayBlock(tlv::security::EncryptedKeyBag,
+ m_encryptedKeyBag.get(),
+ m_encryptedKeyBag.size());
+
+ // Certificate
+ totalLength += this->m_certificate.wireEncode(encoder);
+
+ totalLength += encoder.prependVarNumber(totalLength);
+ totalLength += encoder.prependVarNumber(tlv::security::SafeBag);
+
+ return totalLength;
+}
+
+template size_t
+SafeBag::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+SafeBag::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+SafeBag::wireEncode() const
+{
+ EncodingEstimator estimator;
+ size_t estimatedSize = wireEncode(estimator);
+
+ EncodingBuffer buffer(estimatedSize, 0);
+ wireEncode(buffer);
+
+ this->m_wire = buffer.block();
+ return m_wire;
+}
+
+void
+SafeBag::wireDecode(const Block& wire)
+{
+ if (wire.type() != tlv::security::SafeBag)
+ BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding safebag"));
+
+ this->m_wire = wire;
+ m_wire.parse();
+
+ Block::element_const_iterator it = m_wire.elements_begin();
+
+ // Certificate must be the first part
+ if (it != m_wire.elements_end()) {
+ this->m_certificate.wireDecode(*it);
+ it++;
+ }
+ else
+ BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding certificate"));
+
+ // EncryptedKeyBag
+ if (it != m_wire.elements_end() && it->type() == tlv::security::EncryptedKeyBag) {
+ this->m_encryptedKeyBag = Buffer(it->value(), it->value_size());
+ it++;
+ }
+ else
+ BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure when decoding encryptedkeybag"));
+
+ // Check if end
+ if (it != m_wire.elements_end())
+ BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV structure after decoding the block"));
+}
+
+} // namespace security
+} // namespace ndn
diff --git a/src/security/safe-bag.hpp b/src/security/safe-bag.hpp
new file mode 100644
index 0000000..12555c9
--- /dev/null
+++ b/src/security/safe-bag.hpp
@@ -0,0 +1,121 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 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.
+ *
+ * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
+ */
+#ifndef NDN_CXX_SECURITY_SAFE_BAG_HPP
+#define NDN_CXX_SECURITY_SAFE_BAG_HPP
+
+#include "../common.hpp"
+#include "../data.hpp"
+#include "../encoding/buffer.hpp"
+#include "../encoding/encoder.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "security-common.hpp"
+
+namespace ndn {
+namespace security {
+
+/** @brief a secured container for sensitive information(certificate, private key)
+ */
+class SafeBag
+{
+public:
+ /**
+ * @brief Create a new empty SafeBag object
+ */
+ SafeBag();
+
+ /**
+ * @brief Create a new SafeBag object from the block
+ */
+ explicit
+ SafeBag(const Block& wire);
+
+ /**
+ * @brief Create a new Safe object with the given certificate and private key
+ *
+ * @param certificate A reference to the certificate data packet
+ * @param encryptedKeyBag A reference to the Buffer of private key in PKCS#8
+ */
+ SafeBag(const Data& certificate,
+ const Buffer& encryptedKeyBag);
+
+ /**
+ * @brief Create a new Safe object with the given certificate and private key
+ *
+ * @param certificate A reference to the certificate data packet
+ * @param encryptedKey A reference to the uint8_t* of private key in PKCS#8
+ * @param encryptedKeyLen The length of the encryptedKey
+ */
+ SafeBag(const Data& certificate,
+ const uint8_t* encryptedKey,
+ size_t encryptedKeyLen);
+
+public:
+ /**
+ * @brief Fast encoding or block size estimation
+ */
+ template<encoding::Tag TAG>
+ size_t
+ wireEncode(EncodingImpl<TAG>& encoder) const;
+
+ /**
+ * @brief Encode to a wire format
+ */
+ const Block&
+ wireEncode() const;
+
+ /**
+ * @brief Decode the input from wire format
+ */
+ void
+ wireDecode(const Block& wire);
+
+public:
+ /**
+ * @brief Get the certificate data packet from safe bag
+ */
+ const Data&
+ getCertificate() const
+ {
+ return m_certificate;
+ }
+
+ /**
+ * @brief Get the private key in PKCS#8 from safe bag
+ */
+ const Buffer&
+ getEncryptedKeyBag() const
+ {
+ return m_encryptedKeyBag;
+ }
+
+private:
+ Data m_certificate;
+ Buffer m_encryptedKeyBag;
+
+ mutable Block m_wire;
+};
+
+} // namespace security
+} // namespace ndn
+
+#endif // NDN_CXX_SECURITY_SAFE_BAG_HPP