blob: d45cd851c5f4336ba3791f1f192877f41f791874 [file] [log] [blame]
Yingdi Yud12fb972015-08-01 17:38:49 -07001/* -*- 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
25namespace ndn {
26namespace security {
27namespace transform {
28
29class HmacFilter::Impl
30{
31public:
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070032#if OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yud12fb972015-08-01 17:38:49 -070033 Impl()
34 {
35 HMAC_CTX_init(&m_context);
36 }
37
38 ~Impl()
39 {
40 HMAC_CTX_cleanup(&m_context);
41 }
42
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070043 operator HMAC_CTX*()
44 {
45 return &m_context;
46 }
47
48private:
Yingdi Yud12fb972015-08-01 17:38:49 -070049 HMAC_CTX m_context;
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070050#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
66private:
67 HMAC_CTX* m_context;
68#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
Yingdi Yud12fb972015-08-01 17:38:49 -070069};
70
71HmacFilter::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 Afanasyev02948ec2016-09-12 18:04:50 -070081 if (HMAC_Init_ex(*m_impl, key, keyLen, algorithm, nullptr) == 0)
Yingdi Yud12fb972015-08-01 17:38:49 -070082 BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot initialize HMAC"));
83}
84
85size_t
86HmacFilter::convert(const uint8_t* buf, size_t size)
87{
Alexander Afanasyev02948ec2016-09-12 18:04:50 -070088 if (HMAC_Update(*m_impl, buf, size) == 0)
Yingdi Yud12fb972015-08-01 17:38:49 -070089 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to update HMAC"));
90
91 return size;
92}
93
94void
95HmacFilter::finalize()
96{
97 auto buffer = make_unique<OBuffer>(EVP_MAX_MD_SIZE);
98 unsigned int mdLen = 0;
99
Alexander Afanasyev02948ec2016-09-12 18:04:50 -0700100 if (HMAC_Final(*m_impl, &(*buffer)[0], &mdLen) == 0)
Yingdi Yud12fb972015-08-01 17:38:49 -0700101 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
109unique_ptr<Transform>
110hmacFilter(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