security: Implement operator<< for v2::Certificate

This commit also implements operator<< for SignatureInfoValue and
KeyLocator.

Change-Id: I71d3840ab63bacf1278d755b5eed9630c5a2f48f
diff --git a/src/encoding/tlv.cpp b/src/encoding/tlv.cpp
new file mode 100644
index 0000000..4de73a1
--- /dev/null
+++ b/src/encoding/tlv.cpp
@@ -0,0 +1,42 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 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 "tlv.hpp"
+
+namespace ndn {
+namespace tlv {
+
+std::ostream&
+operator<<(std::ostream& os, const SignatureTypeValue& signatureType)
+{
+  switch (signatureType) {
+    case SignatureTypeValue::DigestSha256:
+      return os << "DigestSha256";
+    case SignatureTypeValue::SignatureSha256WithRsa:
+      return os << "SignatureSha256WithRsa";
+    case SignatureTypeValue::SignatureSha256WithEcdsa:
+      return os << "SignatureSha256WithEcdsa";
+  }
+  return os << "Unknown Signature Type";
+}
+
+} // namespace tlv
+} // namespace ndn
diff --git a/src/encoding/tlv.hpp b/src/encoding/tlv.hpp
index ad964e2..c9a6520 100644
--- a/src/encoding/tlv.hpp
+++ b/src/encoding/tlv.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -99,6 +99,9 @@
   SignatureSha256WithEcdsa = 3
 };
 
+std::ostream&
+operator<<(std::ostream& os, const SignatureTypeValue& signatureType);
+
 /** @brief TLV codes for SignatureInfo features
  *  @sa docs/tutorials/certificate-format.rst
  */
diff --git a/src/key-locator.cpp b/src/key-locator.cpp
index 151a704..8eef8cc 100644
--- a/src/key-locator.cpp
+++ b/src/key-locator.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -21,6 +21,7 @@
 
 #include "key-locator.hpp"
 #include "encoding/block-helpers.hpp"
+#include "util/string-helper.hpp"
 
 namespace ndn {
 
@@ -189,4 +190,30 @@
   return wireEncode() == other.wireEncode();
 }
 
+std::ostream&
+operator<<(std::ostream& os, const KeyLocator& keyLocator)
+{
+  switch (keyLocator.getType()) {
+    case KeyLocator::KeyLocator_Name: {
+      return os << "Name=" << keyLocator.getName();
+    }
+    case KeyLocator::KeyLocator_KeyDigest: {
+      const size_t MAX_DIGEST_OCTETS_TO_SHOW = 5;
+      const Block& digest = keyLocator.getKeyDigest();
+      os << "KeyDigest=" << toHex(digest.value(), digest.value_size()).substr(0, MAX_DIGEST_OCTETS_TO_SHOW * 2);
+      if (digest.value_size() > MAX_DIGEST_OCTETS_TO_SHOW) {
+        os << "...";
+      }
+      return os;
+    }
+    case KeyLocator::KeyLocator_None: {
+      return os << "None";
+    }
+    case KeyLocator::KeyLocator_Unknown: {
+      return os << "Unknown";
+    }
+  }
+  return os << "Unknown";
+}
+
 } // namespace ndn
diff --git a/src/key-locator.hpp b/src/key-locator.hpp
index 8c206e4..b442854 100644
--- a/src/key-locator.hpp
+++ b/src/key-locator.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -162,6 +162,9 @@
   mutable Block m_wire;
 };
 
+std::ostream&
+operator<<(std::ostream& os, const KeyLocator& keyLocator);
+
 } // namespace ndn
 
 #endif // NDN_KEY_LOCATOR_HPP
diff --git a/src/security/v2/certificate.cpp b/src/security/v2/certificate.cpp
index b6a08ab..dcd6ae0 100644
--- a/src/security/v2/certificate.cpp
+++ b/src/security/v2/certificate.cpp
@@ -23,7 +23,10 @@
  */
 
 #include "certificate.hpp"
+#include "additional-description.hpp"
 #include "../../encoding/block-helpers.hpp"
+#include "../../util/indented-stream.hpp"
+#include "../transform.hpp"
 
 namespace ndn {
 namespace security {
@@ -132,6 +135,52 @@
           certName.get(Certificate::KEY_COMPONENT_OFFSET) == Certificate::KEY_COMPONENT);
 }
 
+std::ostream&
+operator<<(std::ostream& os, const Certificate& cert)
+{
+  os << "Certificate name:\n";
+  os << "  " << cert.getName() << "\n";
+  os << "Validity:\n";
+  {
+    os << "  NotBefore: " << time::toIsoString(cert.getValidityPeriod().getPeriod().first) << "\n";
+    os << "  NotAfter: "  << time::toIsoString(cert.getValidityPeriod().getPeriod().second)  << "\n";
+  }
+
+  try {
+    const Block& info = cert.getSignature().getSignatureInfo().getTypeSpecificTlv(tlv::AdditionalDescription);
+    os << "Additional Description:\n";
+    for (const auto& item : v2::AdditionalDescription(info)) {
+      os << "  " << item.first << ": " << item.second << "\n";
+    }
+  }
+  catch (const SignatureInfo::Error&) {
+    // ignore
+  }
+
+  os << "Public key bits:\n";
+  {
+    util::IndentedStream os2(os, "  ");
+    namespace t = ndn::security::transform;
+    t::bufferSource(cert.getPublicKey().buf(), cert.getPublicKey().size()) >> t::base64Encode() >> t::streamSink(os2);
+  }
+
+  os << "Signature Information:\n";
+  {
+    os << "  Signature Type: " << static_cast<tlv::SignatureTypeValue>(cert.getSignature().getType()) << "\n";
+
+    if (cert.getSignature().hasKeyLocator()) {
+      os << "  Key Locator: ";
+      const KeyLocator& keyLocator = cert.getSignature().getKeyLocator();
+      if (keyLocator.getType() == KeyLocator::KeyLocator_Name && keyLocator.getName() == cert.getKeyName()) {
+        os << "Self-Signed ";
+      }
+      os << keyLocator << "\n";
+    }
+  }
+
+  return os;
+}
+
 Name
 extractIdentityFromCertName(const Name& certName)
 {
diff --git a/src/security/v2/certificate.hpp b/src/security/v2/certificate.hpp
index c451277..c5e2c97 100644
--- a/src/security/v2/certificate.hpp
+++ b/src/security/v2/certificate.hpp
@@ -172,6 +172,9 @@
   static const name::Component KEY_COMPONENT;
 };
 
+std::ostream&
+operator<<(std::ostream& os, const Certificate& cert);
+
 /**
  * @brief Extract identity namespace from the certificate name @p certName
  */