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
diff --git a/tests/unit-tests/security/safe-bag.t.cpp b/tests/unit-tests/security/safe-bag.t.cpp
new file mode 100644
index 0000000..cdfe27d
--- /dev/null
+++ b/tests/unit-tests/security/safe-bag.t.cpp
@@ -0,0 +1,169 @@
+/* -*- 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 "security/safe-bag.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Security)
+BOOST_AUTO_TEST_SUITE(TestSafeBag)
+
+const uint8_t CERT[] = {
+  0x06, 0xc8, // Data
+      0x07, 0x14, // Name
+          0x08, 0x05,
+              0x6c, 0x6f, 0x63, 0x61, 0x6c,
+          0x08, 0x03,
+              0x6e, 0x64, 0x6e,
+          0x08, 0x06,
+              0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
+      0x14, 0x07, // MetaInfo
+          0x18, 0x01, // ContentType
+              0x02,
+          0x19, 0x02, // FreshnessPeriod
+              0x27, 0x10,
+      0x15, 0x08, // Content
+          0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
+      0x16, 0x1b, // SignatureInfo
+          0x1b, 0x01, // SignatureType
+              0x01,
+          0x1c, 0x16, // KeyLocator
+              0x07, 0x14, // Name
+                  0x08, 0x04,
+                      0x74, 0x65, 0x73, 0x74,
+                  0x08, 0x03,
+                      0x6b, 0x65, 0x79,
+                  0x08, 0x07,
+                      0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
+      0x17, 0x80, // SignatureValue
+          0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
+          0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
+          0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
+          0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
+          0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
+          0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
+          0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
+          0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
+          0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
+          0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
+};
+
+const uint8_t ENCRYPTED_KEY_BAG[] = {
+  0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
+};
+
+const uint8_t SAFE_BAG[] = {
+  0x80, 0xd4, // SafeBag
+      0x06, 0xc8, // Data
+          0x07, 0x14, // Name
+              0x08, 0x05,
+                  0x6c, 0x6f, 0x63, 0x61, 0x6c,
+              0x08, 0x03,
+                  0x6e, 0x64, 0x6e,
+              0x08, 0x06,
+                  0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
+          0x14, 0x07, // MetaInfo
+              0x18, 0x01, // ContentType
+                  0x02,
+              0x19, 0x02, // FreshnessPeriod
+                  0x27, 0x10,
+          0x15, 0x08, // Content
+              0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21,
+          0x16, 0x1b, // SignatureInfo
+              0x1b, 0x01, // SignatureType
+                  0x01,
+              0x1c, 0x16, // KeyLocator
+                  0x07, 0x14, // Name
+                      0x08, 0x04,
+                          0x74, 0x65, 0x73, 0x74,
+                      0x08, 0x03,
+                          0x6b, 0x65, 0x79,
+                      0x08, 0x07,
+                          0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
+          0x17, 0x80, // SignatureValue
+              0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
+              0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
+              0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
+              0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
+              0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
+              0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
+              0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
+              0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
+              0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
+              0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1,
+      0x81, 0x08, // EncryptedKeyBag
+          0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe
+};
+
+BOOST_AUTO_TEST_CASE(Constructor)
+{
+  Block dataBlock(CERT, sizeof(CERT));
+  Data data(dataBlock);
+  SafeBag safeBag1(data, ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
+
+  Block safeBagBlock(SAFE_BAG, sizeof(SAFE_BAG));
+  SafeBag safeBag2(safeBagBlock);
+
+  Buffer buffer(ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
+  SafeBag safeBag3(data, buffer);
+
+  BOOST_CHECK(safeBag1.getCertificate() == data);
+  BOOST_CHECK(safeBag1.getEncryptedKeyBag() == buffer);
+  BOOST_CHECK(safeBag2.getCertificate() == data);
+  BOOST_CHECK(safeBag2.getEncryptedKeyBag() == buffer);
+  BOOST_CHECK(safeBag3.getCertificate() == data);
+  BOOST_CHECK(safeBag3.getEncryptedKeyBag() == buffer);
+}
+
+BOOST_AUTO_TEST_CASE(EncoderAndDecoder)
+{
+  Block dataBlock(CERT, sizeof(CERT));
+  Data data(dataBlock);
+  SafeBag safeBag(data, ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
+
+  // wire encode
+  Block wireBlock = safeBag.wireEncode();
+  Block block(SAFE_BAG, sizeof(SAFE_BAG));
+
+  // check safe bag block
+  BOOST_CHECK(wireBlock == block);
+
+  // wire decode
+  SafeBag safeBag2;
+  safeBag2.wireDecode(wireBlock);
+
+  // check equal
+  Buffer buffer1 = safeBag2.getEncryptedKeyBag();
+  Buffer buffer2(ENCRYPTED_KEY_BAG, sizeof(ENCRYPTED_KEY_BAG));
+  BOOST_CHECK(buffer1 == buffer2);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestSafeBag
+BOOST_AUTO_TEST_SUITE_END() // Security
+
+} // namespace tests
+} // namespace security
+} // namespace ndn