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-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 
24 #include "ndn-cxx/security/impl/openssl-helper.hpp"
25 
26 #include <boost/lexical_cast.hpp>
27 
28 namespace ndn {
29 namespace security {
30 namespace transform {
31 
32 class VerifierFilter::Impl
33 {
34 public:
35  Impl(const uint8_t* sig, size_t siglen)
36  : sig(sig)
37  , siglen(siglen)
38  {
39  }
40 
41 public:
42  detail::EvpMdCtx ctx;
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  NDN_THROW(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  NDN_THROW(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  NDN_THROW(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, m_impl->sig, m_impl->siglen);
79 
80  auto buffer = make_unique<OBuffer>(1);
81  (*buffer)[0] = (res == 1) ? 1 : 0;
82  setOutputBuffer(std::move(buffer));
83 
85 }
86 
87 unique_ptr<Transform>
89  const uint8_t* sig, size_t sigLen)
90 {
91  return make_unique<VerifierFilter>(algo, key, sig, sigLen);
92 }
93 
94 } // namespace transform
95 } // namespace security
96 } // namespace ndn
Definition: data.cpp:26
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.
#define NDN_THROW(e)
Definition: exception.hpp:61
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
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)
KeyType getKeyType() const
Get the type of the public key.
Definition: public-key.cpp:72
size_t getIndex() const
Get the module index.