signer-filter.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 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 
24 #include "ndn-cxx/security/impl/openssl-helper.hpp"
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn::security::transform {
29 
30 class SignerFilter::Impl
31 {
32 public:
33  detail::EvpMdCtx ctx;
34 };
35 
36 
38  : m_impl(make_unique<Impl>())
39 {
40  const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
41  if (md == nullptr)
42  NDN_THROW(Error(getIndex(), "Unsupported digest algorithm " +
43  boost::lexical_cast<std::string>(algo)));
44 
45  if (EVP_DigestSignInit(m_impl->ctx, nullptr, md, nullptr,
46  reinterpret_cast<EVP_PKEY*>(key.getEvpPkey())) != 1)
47  NDN_THROW(Error(getIndex(), "Failed to initialize signing context with " +
48  boost::lexical_cast<std::string>(algo) + " digest and " +
49  boost::lexical_cast<std::string>(key.getKeyType()) + " key"));
50 }
51 
52 SignerFilter::~SignerFilter() = default;
53 
54 size_t
55 SignerFilter::convert(span<const uint8_t> buf)
56 {
57  if (EVP_DigestSignUpdate(m_impl->ctx, buf.data(), buf.size()) != 1)
58  NDN_THROW(Error(getIndex(), "Failed to accept more input"));
59 
60  return buf.size();
61 }
62 
63 void
64 SignerFilter::finalize()
65 {
66  size_t sigLen = 0;
67  if (EVP_DigestSignFinal(m_impl->ctx, nullptr, &sigLen) != 1)
68  NDN_THROW(Error(getIndex(), "Failed to estimate buffer length"));
69 
70  auto buffer = make_unique<OBuffer>(sigLen);
71  if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &sigLen) != 1)
72  NDN_THROW(Error(getIndex(), "Failed to finalize signature"));
73 
74  buffer->erase(buffer->begin() + sigLen, buffer->end());
75  setOutputBuffer(std::move(buffer));
76 
78 }
79 
80 unique_ptr<Transform>
82 {
83  return make_unique<SignerFilter>(algo, key);
84 }
85 
86 } // namespace ndn::security::transform
size_t getIndex() const
Get the module index.
Base class of transformation error.
Abstraction of a private key in crypto transformations.
Definition: private-key.hpp:39
KeyType getKeyType() const
Returns the type of the private key.
Definition: private-key.cpp:79
SignerFilter(DigestAlgorithm algo, const PrivateKey &key)
Create a module to sign using digest algorithm algo and private key key.
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
#define NDN_THROW(e)
Definition: exception.hpp:56
unique_ptr< Transform > signerFilter(DigestAlgorithm algo, const PrivateKey &key)