security: Add digest calculation support in transformation
Change-Id: I8326c19aff580289b5301687351d81fd74af8a83
Refs: #3009
diff --git a/src/security/detail/openssl-helper.cpp b/src/security/detail/openssl-helper.cpp
new file mode 100644
index 0000000..25fddc7
--- /dev/null
+++ b/src/security/detail/openssl-helper.cpp
@@ -0,0 +1,41 @@
+/* -*- 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 "openssl-helper.hpp"
+
+namespace ndn {
+namespace security {
+namespace detail {
+
+const EVP_MD*
+toDigestEvpMd(DigestAlgorithm algo)
+{
+ switch (algo) {
+ case DIGEST_ALGORITHM_SHA256:
+ return EVP_sha256();
+ default:
+ return nullptr;
+ }
+}
+
+} // namespace detail
+} // namespace security
+} // namespace ndn
diff --git a/src/security/detail/openssl-helper.hpp b/src/security/detail/openssl-helper.hpp
new file mode 100644
index 0000000..e9e5900
--- /dev/null
+++ b/src/security/detail/openssl-helper.hpp
@@ -0,0 +1,39 @@
+/* -*- 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_DETAIL_OPENSSL_HELPER_HPP
+#define NDN_CXX_SECURITY_DETAIL_OPENSSL_HELPER_HPP
+
+#include "../security-common.hpp"
+#include "openssl.hpp"
+
+namespace ndn {
+namespace security {
+namespace detail {
+
+const EVP_MD*
+toDigestEvpMd(DigestAlgorithm algo);
+
+} // namespace detail
+} // namespace security
+} // namespace ndn
+
+#endif // NDN_CXX_SECURITY_DETAIL_OPENSSL_HELPER_HPP
diff --git a/src/security/security-common.hpp b/src/security/security-common.hpp
index ad3f671..8edb22c 100644
--- a/src/security/security-common.hpp
+++ b/src/security/security-common.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).
*
@@ -22,40 +22,41 @@
#ifndef NDN_SECURITY_COMMON_HPP
#define NDN_SECURITY_COMMON_HPP
+#include "../common.hpp"
+
namespace ndn {
-namespace signed_interest{
+namespace signed_interest {
-enum {
- POS_SIG_VALUE = -1,
- POS_SIG_INFO = -2,
- POS_RANDOM_VAL = -3,
- POS_TIMESTAMP = -4,
+const ssize_t POS_SIG_VALUE = -1;
+const ssize_t POS_SIG_INFO = -2;
+const ssize_t POS_RANDOM_VAL = -3;
+const ssize_t POS_TIMESTAMP = -4;
- MIN_LENGTH = 4
-};
+const size_t MIN_LENGTH = 4;
} // namespace signed_interest
enum KeyType {
- KEY_TYPE_RSA = 0,
- KEY_TYPE_ECDSA = 1,
- // KEY_TYPE_DSA,
- KEY_TYPE_AES = 128,
- // KEY_TYPE_DES,
- // KEY_TYPE_RC4,
- // KEY_TYPE_RC2
- KEY_TYPE_NULL = 255
+ KEY_TYPE_NONE = 0,
+ /// @deprecated use KEY_TYPE_NONE
+ KEY_TYPE_NULL = KEY_TYPE_NONE,
+
+ KEY_TYPE_RSA = 1,
+ KEY_TYPE_ECDSA = 2,
+ KEY_TYPE_AES = 128
};
enum KeyClass {
+ KEY_CLASS_NONE,
KEY_CLASS_PUBLIC,
KEY_CLASS_PRIVATE,
KEY_CLASS_SYMMETRIC
};
enum DigestAlgorithm {
- DIGEST_ALGORITHM_SHA256
+ DIGEST_ALGORITHM_NONE = 0,
+ DIGEST_ALGORITHM_SHA256 = 1
};
enum EncryptMode {
@@ -65,10 +66,11 @@
};
enum AclType {
+ ACL_TYPE_NONE,
ACL_TYPE_PUBLIC,
ACL_TYPE_PRIVATE
};
} // namespace ndn
-#endif
+#endif // NDN_SECURITY_COMMON_HPP
diff --git a/src/security/transform.hpp b/src/security/transform.hpp
index b9a960c..879902e 100644
--- a/src/security/transform.hpp
+++ b/src/security/transform.hpp
@@ -33,5 +33,6 @@
#include "transform/hex-decode.hpp"
#include "transform/base64-encode.hpp"
#include "transform/base64-decode.hpp"
+#include "transform/digest-filter.hpp"
#endif // NDN_CXX_SECURITY_TRANSFORM_HPP
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