blob: 2fbe647b61c192fd19068cabd5d0ef30d5fc697c [file] [log] [blame]
Yingdi Yub263f152015-07-12 16:50:13 -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 Yub263f152015-07-12 16:50:13 -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 "signer-filter.hpp"
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040023#include "private-key.hpp"
Davide Pesavento87039532017-09-16 15:15:39 -040024#include "../detail/openssl-helper.hpp"
25
26#include <boost/lexical_cast.hpp>
Yingdi Yub263f152015-07-12 16:50:13 -070027
28namespace ndn {
29namespace security {
30namespace transform {
31
32class SignerFilter::Impl
33{
34public:
Davide Pesavento3504cc42017-09-17 15:28:10 -040035 detail::EvpMdCtx ctx;
Yingdi Yub263f152015-07-12 16:50:13 -070036};
37
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040038
Yingdi Yub263f152015-07-12 16:50:13 -070039SignerFilter::SignerFilter(DigestAlgorithm algo, const PrivateKey& key)
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040040 : m_impl(make_unique<Impl>())
Yingdi Yub263f152015-07-12 16:50:13 -070041{
Davide Pesavento87039532017-09-16 15:15:39 -040042 const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
43 if (md == nullptr)
44 BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " +
45 boost::lexical_cast<std::string>(algo)));
Yingdi Yub263f152015-07-12 16:50:13 -070046
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040047 if (EVP_DigestSignInit(m_impl->ctx, nullptr, md, nullptr,
48 reinterpret_cast<EVP_PKEY*>(key.getEvpPkey())) != 1)
49 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to initialize signing context with " +
50 boost::lexical_cast<std::string>(algo) + " digest and " +
51 boost::lexical_cast<std::string>(key.getKeyType()) + " key"));
Yingdi Yub263f152015-07-12 16:50:13 -070052}
53
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040054SignerFilter::~SignerFilter() = default;
55
Yingdi Yub263f152015-07-12 16:50:13 -070056size_t
57SignerFilter::convert(const uint8_t* buf, size_t size)
58{
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040059 if (EVP_DigestSignUpdate(m_impl->ctx, buf, size) != 1)
60 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
Yingdi Yub263f152015-07-12 16:50:13 -070061
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040062 return size;
Yingdi Yub263f152015-07-12 16:50:13 -070063}
64
65void
66SignerFilter::finalize()
67{
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040068 size_t sigLen = 0;
69 if (EVP_DigestSignFinal(m_impl->ctx, nullptr, &sigLen) != 1)
70 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to estimate buffer length"));
Yingdi Yub263f152015-07-12 16:50:13 -070071
Davide Pesavento06f1bdf2017-09-16 18:59:15 -040072 auto buffer = make_unique<OBuffer>(sigLen);
73 if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &sigLen) != 1)
74 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to finalize signature"));
Yingdi Yub263f152015-07-12 16:50:13 -070075
76 buffer->erase(buffer->begin() + sigLen, buffer->end());
77 setOutputBuffer(std::move(buffer));
78
79 flushAllOutput();
80}
81
82unique_ptr<Transform>
83signerFilter(DigestAlgorithm algo, const PrivateKey& key)
84{
85 return make_unique<SignerFilter>(algo, key);
86}
87
88} // namespace transform
89} // namespace security
90} // namespace ndn