NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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 
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn {
29 namespace security {
30 namespace transform {
31 
33 {
34 public:
36 };
37 
38 
40  : m_impl(make_unique<Impl>())
41 {
42  const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
43  if (md == nullptr)
44  NDN_THROW(Error(getIndex(), "Unsupported digest algorithm " +
45  boost::lexical_cast<std::string>(algo)));
46 
47  if (EVP_DigestSignInit(m_impl->ctx, nullptr, md, nullptr,
48  reinterpret_cast<EVP_PKEY*>(key.getEvpPkey())) != 1)
49  NDN_THROW(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"));
52 }
53 
54 SignerFilter::~SignerFilter() = default;
55 
56 size_t
57 SignerFilter::convert(const uint8_t* buf, size_t size)
58 {
59  if (EVP_DigestSignUpdate(m_impl->ctx, buf, size) != 1)
60  NDN_THROW(Error(getIndex(), "Failed to accept more input"));
61 
62  return size;
63 }
64 
65 void
66 SignerFilter::finalize()
67 {
68  size_t sigLen = 0;
69  if (EVP_DigestSignFinal(m_impl->ctx, nullptr, &sigLen) != 1)
70  NDN_THROW(Error(getIndex(), "Failed to estimate buffer length"));
71 
72  auto buffer = make_unique<OBuffer>(sigLen);
73  if (EVP_DigestSignFinal(m_impl->ctx, buffer->data(), &sigLen) != 1)
74  NDN_THROW(Error(getIndex(), "Failed to finalize signature"));
75 
76  buffer->erase(buffer->begin() + sigLen, buffer->end());
77  setOutputBuffer(std::move(buffer));
78 
80 }
81 
82 unique_ptr<Transform>
84 {
85  return make_unique<SignerFilter>(algo, key);
86 }
87 
88 } // namespace transform
89 } // namespace security
90 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
transform
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
sigLen
size_t sigLen
Definition: verification-helpers.cpp:50
ndn::security::transform::Transform::flushAllOutput
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
Definition: transform-base.cpp:96
ndn::security::transform::signerFilter
unique_ptr< Transform > signerFilter(DigestAlgorithm algo, const PrivateKey &key)
Definition: signer-filter.cpp:83
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::transform::Error
Base class of transformation error.
Definition: transform-base.hpp:49
ndn::security::detail::digestAlgorithmToEvpMd
const EVP_MD * digestAlgorithmToEvpMd(DigestAlgorithm algo)
Definition: openssl-helper.cpp:29
private-key.hpp
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
openssl-helper.hpp
ndn::security::transform::Transform::setOutputBuffer
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
Definition: transform-base.cpp:104
ndn::security::transform::SignerFilter::SignerFilter
SignerFilter(DigestAlgorithm algo, const PrivateKey &key)
Create a module to sign using digest algorithm algo and private key key.
Definition: signer-filter.cpp:39
signer-filter.hpp
ndn::security::transform::Downstream::getIndex
size_t getIndex() const
Get the module index.
Definition: transform-base.hpp:126
ndn::security::transform::PrivateKey::getKeyType
KeyType getKeyType() const
Returns the type of the private key.
Definition: private-key.cpp:89
ndn::security::transform::SignerFilter::Impl
Definition: signer-filter.cpp:33
ndn::security::transform::SignerFilter::~SignerFilter
~SignerFilter()
ndn::security::transform::PrivateKey
Abstraction of private key in crypto transformation.
Definition: private-key.hpp:39
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::security::detail::EvpMdCtx
Definition: openssl-helper.hpp:39
ndn::security::transform::SignerFilter::Impl::ctx
detail::EvpMdCtx ctx
Definition: signer-filter.cpp:35