Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "digest-filter.hpp" |
| 23 | #include "../../encoding/buffer.hpp" |
| 24 | #include "../detail/openssl-helper.hpp" |
| 25 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace transform { |
| 31 | |
| 32 | /** |
| 33 | * @brief The implementation class which contains the internal state of |
| 34 | * the digest calculator which includes openssl specific structures. |
| 35 | */ |
| 36 | class DigestFilter::Impl |
| 37 | { |
| 38 | public: |
| 39 | Impl() |
| 40 | : m_md(BIO_new(BIO_f_md())) |
| 41 | , m_sink(BIO_new(BIO_s_null())) |
| 42 | { |
| 43 | BIO_push(m_md, m_sink); |
| 44 | } |
| 45 | |
| 46 | ~Impl() |
| 47 | { |
| 48 | BIO_free_all(m_md); |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | BIO* m_md; |
| 53 | BIO* m_sink; |
| 54 | }; |
| 55 | |
| 56 | DigestFilter::DigestFilter(DigestAlgorithm algo) |
| 57 | : m_impl(new Impl) |
| 58 | { |
| 59 | const EVP_MD* md = detail::toDigestEvpMd(algo); |
| 60 | if (md == nullptr) { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 61 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " + |
| 62 | boost::lexical_cast<std::string>(algo))); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (!BIO_set_md(m_impl->m_md, md)) { |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 66 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot set digest"+ |
| 67 | boost::lexical_cast<std::string>(algo))); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | size_t |
| 72 | DigestFilter::convert(const uint8_t* buf, size_t size) |
| 73 | { |
| 74 | int wLen = BIO_write(m_impl->m_md, buf, size); |
| 75 | |
| 76 | if (wLen <= 0) { // fail to write data |
| 77 | if (!BIO_should_retry(m_impl->m_md)) { |
| 78 | // we haven't written everything but some error happens, and we cannot retry |
| 79 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input")); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | else { // update number of bytes written |
| 84 | return wLen; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | DigestFilter::finalize() |
| 90 | { |
| 91 | auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE); |
| 92 | |
| 93 | int mdLen = BIO_gets(m_impl->m_md, reinterpret_cast<char*>(&(*buffer)[0]), EVP_MAX_MD_SIZE); |
| 94 | if (mdLen <= 0) |
| 95 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to compute digest")); |
| 96 | |
| 97 | buffer->erase(buffer->begin() + mdLen, buffer->end()); |
| 98 | setOutputBuffer(std::move(buffer)); |
| 99 | |
| 100 | flushAllOutput(); |
| 101 | } |
| 102 | |
| 103 | unique_ptr<Transform> |
| 104 | digestFilter(DigestAlgorithm algo) |
| 105 | { |
| 106 | return make_unique<DigestFilter>(algo); |
| 107 | } |
| 108 | |
| 109 | } // namespace transform |
| 110 | } // namespace security |
| 111 | } // namespace ndn |