blob: c984b8c7e1051eddfe8d981a7003f0aa7b74ec05 [file] [log] [blame]
Yingdi Yuae734272015-07-04 17:38:48 -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 "digest-filter.hpp"
23#include "../../encoding/buffer.hpp"
24#include "../detail/openssl-helper.hpp"
25
Yingdi Yu99b2a002015-08-12 12:47:44 -070026#include <boost/lexical_cast.hpp>
27
Yingdi Yuae734272015-07-04 17:38:48 -070028namespace ndn {
29namespace security {
30namespace transform {
31
32/**
33 * @brief The implementation class which contains the internal state of
34 * the digest calculator which includes openssl specific structures.
35 */
36class DigestFilter::Impl
37{
38public:
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
51public:
52 BIO* m_md;
53 BIO* m_sink;
54};
55
56DigestFilter::DigestFilter(DigestAlgorithm algo)
57 : m_impl(new Impl)
58{
59 const EVP_MD* md = detail::toDigestEvpMd(algo);
60 if (md == nullptr) {
Yingdi Yu99b2a002015-08-12 12:47:44 -070061 BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " +
62 boost::lexical_cast<std::string>(algo)));
Yingdi Yuae734272015-07-04 17:38:48 -070063 }
64
65 if (!BIO_set_md(m_impl->m_md, md)) {
Yingdi Yu99b2a002015-08-12 12:47:44 -070066 BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot set digest"+
67 boost::lexical_cast<std::string>(algo)));
Yingdi Yuae734272015-07-04 17:38:48 -070068 }
69}
70
71size_t
72DigestFilter::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
88void
89DigestFilter::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
103unique_ptr<Transform>
104digestFilter(DigestAlgorithm algo)
105{
106 return make_unique<DigestFilter>(algo);
107}
108
109} // namespace transform
110} // namespace security
111} // namespace ndn