security: Add digest calculation support in transformation
Change-Id: I8326c19aff580289b5301687351d81fd74af8a83
Refs: #3009
diff --git a/src/security/transform/digest-filter.cpp b/src/security/transform/digest-filter.cpp
new file mode 100644
index 0000000..9fed4d2
--- /dev/null
+++ b/src/security/transform/digest-filter.cpp
@@ -0,0 +1,109 @@
+/* -*- 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.
+ */
+
+#include "digest-filter.hpp"
+#include "../../encoding/buffer.hpp"
+#include "../detail/openssl-helper.hpp"
+
+namespace ndn {
+namespace security {
+namespace transform {
+
+/**
+ * @brief The implementation class which contains the internal state of
+ * the digest calculator which includes openssl specific structures.
+ */
+class DigestFilter::Impl
+{
+public:
+ Impl()
+ : m_md(BIO_new(BIO_f_md()))
+ , m_sink(BIO_new(BIO_s_null()))
+ {
+ BIO_push(m_md, m_sink);
+ }
+
+ ~Impl()
+ {
+ BIO_free_all(m_md);
+ }
+
+public:
+ BIO* m_md;
+ BIO* m_sink;
+};
+
+DigestFilter::DigestFilter(DigestAlgorithm algo)
+ : m_impl(new Impl)
+{
+ const EVP_MD* md = detail::toDigestEvpMd(algo);
+ if (md == nullptr) {
+ // @todo Add digest algorithm to the error message
+ BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm"));
+ }
+
+ if (!BIO_set_md(m_impl->m_md, md)) {
+ // @todo Add digest algorithm to the error message
+ BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot set digest"));
+ }
+}
+
+size_t
+DigestFilter::convert(const uint8_t* buf, size_t size)
+{
+ int wLen = BIO_write(m_impl->m_md, buf, size);
+
+ if (wLen <= 0) { // fail to write data
+ if (!BIO_should_retry(m_impl->m_md)) {
+ // we haven't written everything but some error happens, and we cannot retry
+ BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
+ }
+ return 0;
+ }
+ else { // update number of bytes written
+ return wLen;
+ }
+}
+
+void
+DigestFilter::finalize()
+{
+ auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
+
+ int mdLen = BIO_gets(m_impl->m_md, reinterpret_cast<char*>(&(*buffer)[0]), EVP_MAX_MD_SIZE);
+ if (mdLen <= 0)
+ BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to compute digest"));
+
+ buffer->erase(buffer->begin() + mdLen, buffer->end());
+ setOutputBuffer(std::move(buffer));
+
+ flushAllOutput();
+}
+
+unique_ptr<Transform>
+digestFilter(DigestAlgorithm algo)
+{
+ return make_unique<DigestFilter>(algo);
+}
+
+} // namespace transform
+} // namespace security
+} // namespace ndn
diff --git a/src/security/transform/digest-filter.hpp b/src/security/transform/digest-filter.hpp
new file mode 100644
index 0000000..fdd9c5b
--- /dev/null
+++ b/src/security/transform/digest-filter.hpp
@@ -0,0 +1,71 @@
+/* -*- 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.
+ */
+
+#ifndef NDN_CXX_SECURITY_TRANSFORM_DIGEST_FILTER_HPP
+#define NDN_CXX_SECURITY_TRANSFORM_DIGEST_FILTER_HPP
+
+#include "transform-base.hpp"
+#include "../security-common.hpp"
+
+namespace ndn {
+namespace security {
+namespace transform {
+
+/**
+ * @brief The module to calculate digest.
+ */
+class DigestFilter : public Transform
+{
+public:
+ /**
+ * @brief Create a digest module with algorithm @p algo
+ */
+ explicit
+ DigestFilter(DigestAlgorithm algo);
+
+private:
+ /**
+ * @brief Append data @p buf into digest calculation
+ *
+ * @return The number of bytes that have been accepted
+ */
+ virtual size_t
+ convert(const uint8_t* buf, size_t size) final;
+
+ /**
+ * @brief Finalize digest calculation and write the digest into next module.
+ */
+ virtual void
+ finalize() final;
+
+private:
+ class Impl;
+ unique_ptr<Impl> m_impl;
+};
+
+unique_ptr<Transform>
+digestFilter(DigestAlgorithm algo = DIGEST_ALGORITHM_SHA256);
+
+} // namespace transform
+} // namespace security
+} // namespace ndn
+
+#endif // NDN_CXX_SECURITY_TRANSFORM_DIGEST_FILTER_HPP
diff --git a/src/security/transform/transform-base.cpp b/src/security/transform/transform-base.cpp
index 936e0ae..60e967b 100644
--- a/src/security/transform/transform-base.cpp
+++ b/src/security/transform/transform-base.cpp
@@ -92,6 +92,14 @@
}
void
+Transform::flushAllOutput()
+{
+ while (!isOutputBufferEmpty()) {
+ flushOutputBuffer();
+ }
+}
+
+void
Transform::setOutputBuffer(unique_ptr<OBuffer> buffer)
{
BOOST_ASSERT(isOutputBufferEmpty());
@@ -139,9 +147,7 @@
void
Transform::finalize()
{
- while (!isOutputBufferEmpty()) {
- flushOutputBuffer();
- }
+ flushAllOutput();
}
Source::Source()
diff --git a/src/security/transform/transform-base.hpp b/src/security/transform/transform-base.hpp
index 5e3a72b..98faf6d 100644
--- a/src/security/transform/transform-base.hpp
+++ b/src/security/transform/transform-base.hpp
@@ -186,11 +186,20 @@
/**
* @brief Read the content from output buffer and write it into next module.
+ *
+ * It is not guaranteed that all the content in output buffer will be flushed to next module.
*/
void
flushOutputBuffer();
/**
+ * @brief Read the all the content from output buffer and write it into next module.
+ * @post isOutputBufferEmpty() returns true.
+ */
+ void
+ flushAllOutput();
+
+ /**
* @brief Set output buffer to @p buffer
*/
void