NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
verifier-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-2017 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 "verifier-filter.hpp"
23 #include "public-key.hpp"
24 #include "../detail/openssl-helper.hpp"
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn {
29 namespace security {
30 namespace transform {
31 
33 {
34 public:
35  Impl(const uint8_t* sig, size_t siglen)
36  : sig(sig)
37  , siglen(siglen)
38  {
39  }
40 
41 public:
43  const uint8_t* sig;
44  size_t siglen;
45 };
46 
47 
49  const uint8_t* sig, size_t sigLen)
50  : m_impl(make_unique<Impl>(sig, sigLen))
51 {
52  const EVP_MD* md = detail::digestAlgorithmToEvpMd(algo);
53  if (md == nullptr)
54  BOOST_THROW_EXCEPTION(Error(getIndex(), "Unsupported digest algorithm " +
55  boost::lexical_cast<std::string>(algo)));
56 
57  if (EVP_DigestVerifyInit(m_impl->ctx, nullptr, md, nullptr,
58  reinterpret_cast<EVP_PKEY*>(key.getEvpPkey())) != 1)
59  BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to initialize verification context with " +
60  boost::lexical_cast<std::string>(algo) + " digest and " +
61  boost::lexical_cast<std::string>(key.getKeyType()) + " key"));
62 }
63 
65 
66 size_t
67 VerifierFilter::convert(const uint8_t* buf, size_t size)
68 {
69  if (EVP_DigestVerifyUpdate(m_impl->ctx, buf, size) != 1)
70  BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
71 
72  return size;
73 }
74 
75 void
76 VerifierFilter::finalize()
77 {
78  int res = EVP_DigestVerifyFinal(m_impl->ctx,
79 #if OPENSSL_VERSION_NUMBER < 0x1000200fL
80  const_cast<uint8_t*>(m_impl->sig),
81 #else
82  m_impl->sig,
83 #endif
84  m_impl->siglen);
85 
86  auto buffer = make_unique<OBuffer>(1);
87  (*buffer)[0] = (res == 1) ? 1 : 0;
88  setOutputBuffer(std::move(buffer));
89 
91 }
92 
93 unique_ptr<Transform>
95  const uint8_t* sig, size_t sigLen)
96 {
97  return make_unique<VerifierFilter>(algo, key, sig, sigLen);
98 }
99 
100 } // namespace transform
101 } // namespace security
102 } // namespace ndn
KeyType getKeyType() const
Get the type of the public key.
Definition: public-key.cpp:72
Copyright (c) 2011-2015 Regents of the University of California.
size_t getIndex() const
Get the module index.
Abstraction of public key in crypto transformation.
Definition: public-key.hpp:35
void flushAllOutput()
Read the all the content from output buffer and write it into next module.
Impl(const uint8_t *sig, size_t siglen)
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
const EVP_MD * digestAlgorithmToEvpMd(DigestAlgorithm algo)
VerifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
Create a verifier module to verify signature sig using algorithm algo and key key.
Base class of transformation error.
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
unique_ptr< T > make_unique(Args &&... args)
Definition: backports.hpp:96