Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 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" |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 23 | #include "../detail/openssl-helper.hpp" |
| 24 | |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | namespace transform { |
| 30 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 31 | class DigestFilter::Impl |
| 32 | { |
| 33 | public: |
Davide Pesavento | 3504cc4 | 2017-09-17 15:28:10 -0400 | [diff] [blame] | 34 | detail::EvpMdCtx ctx; |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 37 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 38 | DigestFilter::DigestFilter(DigestAlgorithm algo) |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 39 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 8703953 | 2017-09-16 15:15:39 -0400 | [diff] [blame] | 41 | const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo); |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 42 | if (md == nullptr) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 43 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " + |
| 44 | boost::lexical_cast<std::string>(algo))); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 46 | if (EVP_DigestInit_ex(m_impl->ctx, md, nullptr) == 0) |
| 47 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot initialize digest " + |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 48 | boost::lexical_cast<std::string>(algo))); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 51 | DigestFilter::~DigestFilter() = default; |
| 52 | |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 53 | size_t |
| 54 | DigestFilter::convert(const uint8_t* buf, size_t size) |
| 55 | { |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 56 | if (EVP_DigestUpdate(m_impl->ctx, buf, size) == 0) |
| 57 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input")); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 58 | |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 59 | return size; |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | DigestFilter::finalize() |
| 64 | { |
| 65 | auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE); |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 66 | unsigned int mdLen = 0; |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 67 | |
Davide Pesavento | 0f5078e | 2017-08-31 17:12:27 -0400 | [diff] [blame] | 68 | if (EVP_DigestFinal_ex(m_impl->ctx, buffer->data(), &mdLen) == 0) |
| 69 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to finalize digest")); |
Yingdi Yu | ae73427 | 2015-07-04 17:38:48 -0700 | [diff] [blame] | 70 | |
| 71 | buffer->erase(buffer->begin() + mdLen, buffer->end()); |
| 72 | setOutputBuffer(std::move(buffer)); |
| 73 | |
| 74 | flushAllOutput(); |
| 75 | } |
| 76 | |
| 77 | unique_ptr<Transform> |
| 78 | digestFilter(DigestAlgorithm algo) |
| 79 | { |
| 80 | return make_unique<DigestFilter>(algo); |
| 81 | } |
| 82 | |
| 83 | } // namespace transform |
| 84 | } // namespace security |
| 85 | } // namespace ndn |