security: Implement operator<< for v2::Certificate

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

Change-Id: I71d3840ab63bacf1278d755b5eed9630c5a2f48f
This commit is contained in:
Alexander Afanasyev committed 2017-01-08 12:58:56 -08:00
1 parent 48becde5b4
commit 5f1820e60f
9 files changed
+195 -10

No files matched your search

+42
View File
@@ -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
+4 -1
View File
@@ -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 @@ enum SignatureTypeValue {
SignatureSha256WithEcdsa = 3
};
std::ostream&
operator<<(std::ostream& os, const SignatureTypeValue& signatureType);
/** @brief TLV codes for SignatureInfo features
* @sa docs/tutorials/certificate-format.rst
*/
+28 -1
View File
@@ -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 @@ KeyLocator::operator==(const KeyLocator& other) const
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
+4 -1
View File
@@ -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 @@ private:
mutable Block m_wire;
};
std::ostream&
operator<<(std::ostream& os, const KeyLocator& keyLocator);
} // namespace ndn
#endif // NDN_KEY_LOCATOR_HPP
+49
View File
@@ -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 @@ Certificate::isValidName(const Name& certName)
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)
{
+3
View File
@@ -172,6 +172,9 @@ public:
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
*/
+22 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2016 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).
*
@@ -25,6 +25,7 @@
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/lexical_cast.hpp>
namespace ndn {
namespace tlv {
@@ -401,6 +402,26 @@ BOOST_AUTO_TEST_CASE(ReadFromStream)
BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
BOOST_AUTO_TEST_SUITE(PrintHelpers)
BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
{
SignatureTypeValue value = DigestSha256;
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
value = SignatureSha256WithRsa;
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
value = SignatureSha256WithEcdsa;
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
value = static_cast<SignatureTypeValue>(-1);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
}
BOOST_AUTO_TEST_SUITE_END() // TestTlv
BOOST_AUTO_TEST_SUITE_END() // TestTlv
BOOST_AUTO_TEST_SUITE_END() // Encoding
+18 -5
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2016 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).
*
@@ -23,6 +23,7 @@
#include "encoding/block-helpers.hpp"
#include "boost-test.hpp"
#include <boost/lexical_cast.hpp>
namespace ndn {
namespace tests {
@@ -56,6 +57,8 @@ BOOST_AUTO_TEST_CASE(TypeNone)
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_None);
BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "None");
}
BOOST_AUTO_TEST_CASE(TypeName)
@@ -86,13 +89,15 @@ BOOST_AUTO_TEST_CASE(TypeName)
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_Name);
BOOST_CHECK_EQUAL(b.getName(), Name("/N"));
BOOST_CHECK_THROW(b.getKeyDigest(), KeyLocator::Error);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Name=/N");
}
BOOST_AUTO_TEST_CASE(TypeKeyDigest)
{
char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets, 8);
std::string digestOctets = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45";
ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets.c_str(), digestOctets.size());
Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets.c_str(), digestOctets.size());
KeyLocator a;
a.setKeyDigest(digestBuffer);
@@ -109,7 +114,7 @@ BOOST_AUTO_TEST_CASE(TypeKeyDigest)
// printf("0x%02x, ", *it);
// }
static const uint8_t expected[] = {
0x1c, 0x0a, 0x1d, 0x08, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
0x1c, 0x0c, 0x1d, 0x0a, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x23, 0x45
};
BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
wire.begin(), wire.end());
@@ -120,6 +125,12 @@ BOOST_AUTO_TEST_CASE(TypeKeyDigest)
BOOST_CHECK_EQUAL(b.getType(), KeyLocator::KeyLocator_KeyDigest);
BOOST_CHECK(b.getKeyDigest() == expectedDigestBlock);
BOOST_CHECK_THROW(b.getName(), KeyLocator::Error);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=123456789A...");
std::string shortDigest = "\xbc\xde\xf1";
b.setKeyDigest(make_shared<Buffer>(shortDigest.c_str(), shortDigest.size()));
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "KeyDigest=BCDEF1");
}
BOOST_AUTO_TEST_CASE(Equality)
@@ -167,6 +178,8 @@ BOOST_AUTO_TEST_CASE(UnknownType)
BOOST_CHECK_EQUAL(a == b, true);
BOOST_CHECK_EQUAL(a != b, false);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "Unknown");
b.setName("/N");
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
+25 -1
View File
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2016 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).
*
@@ -256,6 +256,30 @@ BOOST_FIXTURE_TEST_CASE(EmptyContent, InvalidCertFixture)
BOOST_CHECK_THROW(cert.getPublicKey(), Certificate::Error);
}
BOOST_AUTO_TEST_CASE(PrintCertificateInfo)
{
const std::string expectedCertificateInfo = std::string(R"INFO(
Certificate name:
/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B
Validity:
NotBefore: 20150814T223739
NotAfter: 20150818T223738
Public key bits:
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCeBj5HhbI0N6qFR6wDJIO1nKgF
OiQe64kBu+mbssMirGjj8GwCzmimxNCnBpCcqhsIHYtDmjNnRG0hoxuImpdeWcQV
C9ksvVEHYYKtwbjXv5vPfSTCY/OXF+v+YiW6W02Kwnq9Q4qPuPLxxWow01CMyJrf
7+0153pi6nZ8uwgmxwIBEQ==
Signature Information:
Signature Type: SignatureSha256WithRsa
Key Locator: Name=/ndn/site1/KEY/ksk-2516425377094
)INFO").substr(1);
Certificate certificate(Block(CERT, sizeof(CERT)));
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(certificate), expectedCertificateInfo);
// @todo Check output formats of other certificates
}
BOOST_AUTO_TEST_SUITE_END() // TestCertificate
BOOST_AUTO_TEST_SUITE_END() // V2
BOOST_AUTO_TEST_SUITE_END() // Security