security: Handle certificate decoding error in validation process
Change-Id: I6365db2d9fd58c4dd5d7e1f840cc85253381e03c
Refs: #2212
diff --git a/src/security/certificate.cpp b/src/security/certificate.cpp
index 3af9bf9..67c440b 100644
--- a/src/security/certificate.cpp
+++ b/src/security/certificate.cpp
@@ -30,9 +30,15 @@
#include "cryptopp.hpp"
#include "../encoding/cryptopp/asn_ext.hpp"
#include "../encoding/buffer-stream.hpp"
+#include "../util/concepts.hpp"
namespace ndn {
+BOOST_CONCEPT_ASSERT((WireEncodable<Certificate>));
+BOOST_CONCEPT_ASSERT((WireDecodable<Certificate>));
+static_assert(std::is_base_of<tlv::Error, Certificate::Error>::value,
+ "Certificate::Error must inherit from tlv::Error");
+
Certificate::Certificate()
: m_notBefore(time::system_clock::TimePoint::max())
, m_notAfter(time::system_clock::TimePoint::min())
@@ -46,9 +52,21 @@
decode();
}
+Certificate::Certificate(const Block& block)
+ : Data(block)
+{
+ decode();
+}
+
Certificate::~Certificate()
{
- //TODO:
+}
+
+void
+Certificate::wireDecode(const Block& wire)
+{
+ Data::wireDecode(wire);
+ decode();
}
bool
@@ -177,66 +195,71 @@
{
using namespace CryptoPP;
- OBufferStream os;
- StringSource source(getContent().value(), getContent().value_size(), true);
+ try {
+ OBufferStream os;
+ StringSource source(getContent().value(), getContent().value_size(), true);
- // idCert ::= SEQUENCE {
- // validity Validity,
- // subject Name,
- // subjectPubKeyInfo SubjectPublicKeyInfo,
- // extension Extensions OPTIONAL }
- BERSequenceDecoder idCert(source);
- {
- // Validity ::= SEQUENCE {
- // notBefore Time,
- // notAfter Time }
- BERSequenceDecoder validity(idCert);
+ // idCert ::= SEQUENCE {
+ // validity Validity,
+ // subject Name,
+ // subjectPubKeyInfo SubjectPublicKeyInfo,
+ // extension Extensions OPTIONAL }
+ BERSequenceDecoder idCert(source);
{
- BERDecodeTime(validity, m_notBefore);
- BERDecodeTime(validity, m_notAfter);
- }
- validity.MessageEnd();
-
- // Name ::= CHOICE {
- // RDNSequence }
- //
- // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
- m_subjectDescriptionList.clear();
- BERSequenceDecoder name(idCert);
- {
- while (!name.EndReached())
- {
- m_subjectDescriptionList.push_back(CertificateSubjectDescription(name));
- }
- }
- name.MessageEnd();
-
- // SubjectPublicKeyInfo ::= SEQUENCE {
- // algorithm AlgorithmIdentifier
- // keybits BIT STRING }
- m_key.decode(idCert);
-
- // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
- //
- // Extension ::= SEQUENCE {
- // extnID OBJECT IDENTIFIER,
- // critical BOOLEAN DEFAULT FALSE,
- // extnValue OCTET STRING }
- m_extensionList.clear();
- if (!idCert.EndReached())
+ // Validity ::= SEQUENCE {
+ // notBefore Time,
+ // notAfter Time }
+ BERSequenceDecoder validity(idCert);
{
- BERSequenceDecoder extensions(idCert);
- {
- while (!extensions.EndReached())
- {
- m_extensionList.push_back(CertificateExtension(extensions));
- }
- }
- extensions.MessageEnd();
+ BERDecodeTime(validity, m_notBefore);
+ BERDecodeTime(validity, m_notAfter);
}
- }
+ validity.MessageEnd();
- idCert.MessageEnd();
+ // Name ::= CHOICE {
+ // RDNSequence }
+ //
+ // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
+ m_subjectDescriptionList.clear();
+ BERSequenceDecoder name(idCert);
+ {
+ while (!name.EndReached())
+ {
+ m_subjectDescriptionList.push_back(CertificateSubjectDescription(name));
+ }
+ }
+ name.MessageEnd();
+
+ // SubjectPublicKeyInfo ::= SEQUENCE {
+ // algorithm AlgorithmIdentifier
+ // keybits BIT STRING }
+ m_key.decode(idCert);
+
+ // Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
+ //
+ // Extension ::= SEQUENCE {
+ // extnID OBJECT IDENTIFIER,
+ // critical BOOLEAN DEFAULT FALSE,
+ // extnValue OCTET STRING }
+ m_extensionList.clear();
+ if (!idCert.EndReached())
+ {
+ BERSequenceDecoder extensions(idCert);
+ {
+ while (!extensions.EndReached())
+ {
+ m_extensionList.push_back(CertificateExtension(extensions));
+ }
+ }
+ extensions.MessageEnd();
+ }
+ }
+
+ idCert.MessageEnd();
+ }
+ catch (CryptoPP::BERDecodeErr&) {
+ throw Error("Certificate Decoding Error");
+ }
}
void
@@ -320,4 +343,12 @@
}
+std::ostream&
+operator<<(std::ostream& os, const Certificate& cert)
+{
+ cert.printCertificate(os);
+ return os;
+}
+
+
} // namespace ndn
diff --git a/src/security/certificate.hpp b/src/security/certificate.hpp
index eff2bbc..c0fe8e0 100644
--- a/src/security/certificate.hpp
+++ b/src/security/certificate.hpp
@@ -37,12 +37,12 @@
class Certificate : public Data
{
public:
- class Error : public std::runtime_error
+ class Error : public Data::Error
{
public:
explicit
Error(const std::string& what)
- : std::runtime_error(what)
+ : Data::Error(what)
{
}
};
@@ -51,34 +51,38 @@
typedef std::vector<CertificateExtension> ExtensionList;
/**
- * The default constructor.
+ * @brief The default constructor.
*/
Certificate();
/**
- * Create a Certificate from the content in the data packet.
+ * @brief Create a Certificate from the content in the data packet.
* @param data The data packet with the content to decode.
*/
explicit
Certificate(const Data& data);
/**
- * The virtual destructor.
+ * @brief Create a Certificate from the a block
+ * @param block The raw block of the certificate
*/
+ explicit
+ Certificate(const Block& block);
+
virtual
~Certificate();
- inline void
+ void
wireDecode(const Block& wire);
/**
- * encode certificate info into content
+ * @brief encode certificate info into content
*/
void
encode();
/**
- * Add a subject description.
+ * @brief Add a subject description.
* @param description The description to be added.
*/
void
@@ -100,7 +104,7 @@
}
/**
- * Add a certificate extension.
+ * @brief Add a certificate extension.
* @param extension the extension to be added
*/
void
@@ -176,14 +180,14 @@
}
/**
- * Check if the certificate is valid.
+ * @brief Check if the certificate is valid.
* @return True if the current time is earlier than notBefore.
*/
bool
isTooEarly();
/**
- * Check if the certificate is valid.
+ * @brief Check if the certificate is valid.
* @return True if the current time is later than notAfter.
*/
bool
@@ -204,21 +208,8 @@
ExtensionList m_extensionList;
};
-inline void
-Certificate::wireDecode(const Block& wire)
-{
- Data::wireDecode(wire);
- decode();
-}
-
-
-inline std::ostream&
-operator<<(std::ostream& os, const Certificate& cert)
-{
- cert.printCertificate(os);
- return os;
-}
-
+std::ostream&
+operator<<(std::ostream& os, const Certificate& cert);
} // namespace ndn
-#endif //NDN_SECURITY_CERTIFICATE_HPP
+#endif // NDN_SECURITY_CERTIFICATE_HPP
diff --git a/src/security/identity-certificate.cpp b/src/security/identity-certificate.cpp
index 685fa29..7ec06f9 100644
--- a/src/security/identity-certificate.cpp
+++ b/src/security/identity-certificate.cpp
@@ -24,11 +24,47 @@
#include "common.hpp"
#include "identity-certificate.hpp"
+#include "../util/concepts.hpp"
namespace ndn {
using std::string;
+BOOST_CONCEPT_ASSERT((WireEncodable<IdentityCertificate>));
+BOOST_CONCEPT_ASSERT((WireDecodable<IdentityCertificate>));
+static_assert(std::is_base_of<Certificate::Error, IdentityCertificate::Error>::value,
+ "IdentityCertificate::Error must inherit from Certificate::Error");
+
+IdentityCertificate::IdentityCertificate()
+{
+}
+
+IdentityCertificate::IdentityCertificate(const Data& data)
+ : Certificate(data)
+{
+ setPublicKeyName();
+}
+
+IdentityCertificate::IdentityCertificate(const Block& block)
+ : Certificate(block)
+{
+ setPublicKeyName();
+}
+
+void
+IdentityCertificate::wireDecode(const Block& wire)
+{
+ Certificate::wireDecode(wire);
+ setPublicKeyName();
+}
+
+void
+IdentityCertificate::setName(const Name& name)
+{
+ Certificate::setName(name);
+ setPublicKeyName();
+}
+
bool
IdentityCertificate::isCorrectName(const Name& name)
{
diff --git a/src/security/identity-certificate.hpp b/src/security/identity-certificate.hpp
index 90ad3c5..b416080 100644
--- a/src/security/identity-certificate.hpp
+++ b/src/security/identity-certificate.hpp
@@ -32,33 +32,34 @@
class IdentityCertificate : public Certificate
{
public:
- class Error : public std::runtime_error
+ class Error : public Certificate::Error
{
public:
explicit
Error(const std::string& what)
- : std::runtime_error(what)
+ : Certificate::Error(what)
{
}
};
/**
- * The default constructor.
+ * @brief The default constructor.
*/
IdentityCertificate();
/**
- * Create an IdentityCertificate from the content in the data packet.
+ * @brief Create an IdentityCertificate from the content in the data packet.
* @param data The data packet with the content to decode.
*/
explicit
IdentityCertificate(const Data& data);
/**
- * The virtual destructor.
+ * @brief Create an IdentityCertificate from a block.
+ * @param block The raw block of the certificate.
*/
- virtual
- ~IdentityCertificate();
+ explicit
+ IdentityCertificate(const Block& block);
void
wireDecode(const Block& wire);
@@ -67,13 +68,16 @@
setName(const Name& name);
const Name&
- getPublicKeyName() const;
+ getPublicKeyName() const
+ {
+ return m_publicKeyName;
+ }
static bool
isIdentityCertificate(const Certificate& certificate);
/**
- * Get the public key name from the full certificate name.
+ * @brief Get the public key name from the full certificate name.
* @param certificateName The full certificate name.
* @return The related public key name.
*/
@@ -91,43 +95,6 @@
Name m_publicKeyName;
};
-inline
-IdentityCertificate::IdentityCertificate()
-{
-}
-
-inline
-IdentityCertificate::IdentityCertificate(const Data& data)
- : Certificate(data)
-{
- setPublicKeyName();
-}
-
-inline
-IdentityCertificate::~IdentityCertificate()
-{
-}
-
-inline void
-IdentityCertificate::wireDecode(const Block& wire)
-{
- Certificate::wireDecode(wire);
- setPublicKeyName();
-}
-
-inline void
-IdentityCertificate::setName(const Name& name)
-{
- Certificate::setName(name);
- setPublicKeyName();
-}
-
-inline const Name&
-IdentityCertificate::getPublicKeyName() const
-{
- return m_publicKeyName;
-}
-
} // namespace ndn
-#endif //NDN_SECURITY_IDENTITY_CERTIFICATE_HPP
+#endif // NDN_SECURITY_IDENTITY_CERTIFICATE_HPP
diff --git a/src/security/validator-config.cpp b/src/security/validator-config.cpp
index b55cf69..149c5dc 100644
--- a/src/security/validator-config.cpp
+++ b/src/security/validator-config.cpp
@@ -593,16 +593,17 @@
return onValidationFailed(interest.shared_from_this(),
"No valid KeyLocator");
}
- catch (tlv::Error& e)
- {
- return onValidationFailed(interest.shared_from_this(),
- "Cannot decode signature");
- }
catch (IdentityCertificate::Error& e)
{
return onValidationFailed(interest.shared_from_this(),
"Cannot determine the signing key");
}
+
+ catch (tlv::Error& e)
+ {
+ return onValidationFailed(interest.shared_from_this(),
+ "Cannot decode signature");
+ }
}
void
@@ -831,8 +832,20 @@
const OnValidated& onValidated,
const OnFailed& onValidationFailed)
{
- shared_ptr<IdentityCertificate> certificate =
- make_shared<IdentityCertificate>(*signCertificate);
+ if (signCertificate->getContentType() != tlv::ContentType_Key)
+ return onValidationFailed(packet,
+ "Cannot retrieve signer's cert: " +
+ signCertificate->getName().toUri());
+
+ shared_ptr<IdentityCertificate> certificate;
+ try {
+ certificate = make_shared<IdentityCertificate>(*signCertificate);
+ }
+ catch (tlv::Error&) {
+ return onValidationFailed(packet,
+ "Cannot decode signer's cert: " +
+ signCertificate->getName().toUri());
+ }
if (!certificate->isTooLate() && !certificate->isTooEarly())
{