Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -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 "hmac-filter.hpp" |
| 23 | #include "../detail/openssl-helper.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | namespace transform { |
| 28 | |
| 29 | class HmacFilter::Impl |
| 30 | { |
| 31 | public: |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 32 | #if OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 33 | Impl() |
| 34 | { |
| 35 | HMAC_CTX_init(&m_context); |
| 36 | } |
| 37 | |
| 38 | ~Impl() |
| 39 | { |
| 40 | HMAC_CTX_cleanup(&m_context); |
| 41 | } |
| 42 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 43 | operator HMAC_CTX*() |
| 44 | { |
| 45 | return &m_context; |
| 46 | } |
| 47 | |
| 48 | private: |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 49 | HMAC_CTX m_context; |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 50 | #else |
| 51 | Impl() |
| 52 | : m_context(HMAC_CTX_new()) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | ~Impl() |
| 57 | { |
| 58 | HMAC_CTX_free(m_context); |
| 59 | } |
| 60 | |
| 61 | operator HMAC_CTX*() |
| 62 | { |
| 63 | return m_context; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | HMAC_CTX* m_context; |
| 68 | #endif // OPENSSL_VERSION_NUMBER < 0x1010000fL |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | HmacFilter::HmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen) |
| 72 | : m_impl(new Impl) |
| 73 | { |
| 74 | BOOST_ASSERT(key != nullptr); |
| 75 | BOOST_ASSERT(keyLen > 0); |
| 76 | |
| 77 | const EVP_MD* algorithm = detail::toDigestEvpMd(algo); |
| 78 | if (algorithm == nullptr) |
| 79 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm")); |
| 80 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 81 | if (HMAC_Init_ex(*m_impl, key, keyLen, algorithm, nullptr) == 0) |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 82 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot initialize HMAC")); |
| 83 | } |
| 84 | |
| 85 | size_t |
| 86 | HmacFilter::convert(const uint8_t* buf, size_t size) |
| 87 | { |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 88 | if (HMAC_Update(*m_impl, buf, size) == 0) |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 89 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to update HMAC")); |
| 90 | |
| 91 | return size; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | HmacFilter::finalize() |
| 96 | { |
| 97 | auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE); |
| 98 | unsigned int mdLen = 0; |
| 99 | |
Alexander Afanasyev | 02948ec | 2016-09-12 18:04:50 -0700 | [diff] [blame] | 100 | if (HMAC_Final(*m_impl, &(*buffer)[0], &mdLen) == 0) |
Yingdi Yu | d12fb97 | 2015-08-01 17:38:49 -0700 | [diff] [blame] | 101 | BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to finalize HMAC")); |
| 102 | |
| 103 | buffer->erase(buffer->begin() + mdLen, buffer->end()); |
| 104 | setOutputBuffer(std::move(buffer)); |
| 105 | |
| 106 | flushAllOutput(); |
| 107 | } |
| 108 | |
| 109 | unique_ptr<Transform> |
| 110 | hmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen) |
| 111 | { |
| 112 | return make_unique<HmacFilter>(algo, key, keyLen); |
| 113 | } |
| 114 | |
| 115 | } // namespace transform |
| 116 | } // namespace security |
| 117 | } // namespace ndn |