blob: 12de2ea2deb6d4bec8f9adba600f75cb8cbe936c [file] [log] [blame]
Yingdi Yud12fb972015-08-01 17:38:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87039532017-09-16 15:15:39 -04002/*
3 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yud12fb972015-08-01 17:38:49 -07004 *
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
Davide Pesavento9219f582017-09-17 13:58:29 -040025#include <boost/lexical_cast.hpp>
26
Yingdi Yud12fb972015-08-01 17:38:49 -070027namespace ndn {
28namespace security {
29namespace transform {
30
31class HmacFilter::Impl
32{
33public:
Davide Pesavento3504cc42017-09-17 15:28:10 -040034 Impl()
Davide Pesavento9219f582017-09-17 13:58:29 -040035 : key(nullptr)
36 {
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070037 }
38
39 ~Impl()
40 {
Davide Pesavento9219f582017-09-17 13:58:29 -040041 EVP_PKEY_free(key);
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070042 }
43
Davide Pesavento9219f582017-09-17 13:58:29 -040044public:
Davide Pesavento3504cc42017-09-17 15:28:10 -040045 detail::EvpMdCtx ctx;
Davide Pesavento9219f582017-09-17 13:58:29 -040046 EVP_PKEY* key;
Yingdi Yud12fb972015-08-01 17:38:49 -070047};
48
Davide Pesavento8aad3722017-09-16 20:57:28 -040049
Yingdi Yud12fb972015-08-01 17:38:49 -070050HmacFilter::HmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen)
Davide Pesavento8aad3722017-09-16 20:57:28 -040051 : m_impl(make_unique<Impl>())
Yingdi Yud12fb972015-08-01 17:38:49 -070052{
53 BOOST_ASSERT(key != nullptr);
54 BOOST_ASSERT(keyLen > 0);
55
Davide Pesavento9219f582017-09-17 13:58:29 -040056 const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
57 if (md == nullptr)
58 BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " +
59 boost::lexical_cast<std::string>(algo)));
Yingdi Yud12fb972015-08-01 17:38:49 -070060
Davide Pesavento9219f582017-09-17 13:58:29 -040061 m_impl->key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, nullptr, key, static_cast<int>(keyLen));
62 if (m_impl->key == nullptr)
63 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to create HMAC key"));
64
65 if (EVP_DigestSignInit(m_impl->ctx, nullptr, md, nullptr, m_impl->key) != 1)
66 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to initialize HMAC context with " +
67 boost::lexical_cast<std::string>(algo) + " digest"));
Yingdi Yud12fb972015-08-01 17:38:49 -070068}
69
Davide Pesavento8aad3722017-09-16 20:57:28 -040070HmacFilter::~HmacFilter() = default;
71
Yingdi Yud12fb972015-08-01 17:38:49 -070072size_t
73HmacFilter::convert(const uint8_t* buf, size_t size)
74{
Davide Pesavento9219f582017-09-17 13:58:29 -040075 if (EVP_DigestSignUpdate(m_impl->ctx, buf, size) != 1)
76 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
Yingdi Yud12fb972015-08-01 17:38:49 -070077
78 return size;
79}
80
81void
82HmacFilter::finalize()
83{
84 auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
Davide Pesavento9219f582017-09-17 13:58:29 -040085 size_t hmacLen = 0;
Yingdi Yud12fb972015-08-01 17:38:49 -070086
Davide Pesavento9219f582017-09-17 13:58:29 -040087 if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &hmacLen) != 1)
Yingdi Yud12fb972015-08-01 17:38:49 -070088 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to finalize HMAC"));
89
Davide Pesavento9219f582017-09-17 13:58:29 -040090 buffer->erase(buffer->begin() + hmacLen, buffer->end());
Yingdi Yud12fb972015-08-01 17:38:49 -070091 setOutputBuffer(std::move(buffer));
92
93 flushAllOutput();
94}
95
96unique_ptr<Transform>
97hmacFilter(DigestAlgorithm algo, const uint8_t* key, size_t keyLen)
98{
99 return make_unique<HmacFilter>(algo, key, keyLen);
100}
101
102} // namespace transform
103} // namespace security
104} // namespace ndn