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
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
48
VerifierFilter::VerifierFilter
(
DigestAlgorithm
algo,
const
PublicKey
& key,
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
64
VerifierFilter::~VerifierFilter
() =
default
;
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
90
flushAllOutput
();
91
}
92
93
unique_ptr<Transform>
94
verifierFilter
(
DigestAlgorithm
algo,
const
PublicKey
& key,
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
ndn::security::transform::PublicKey::getKeyType
KeyType getKeyType() const
Get the type of the public key.
Definition:
public-key.cpp:72
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
verifier-filter.hpp
ndn::security::transform::VerifierFilter::Impl::siglen
size_t siglen
Definition:
verifier-filter.cpp:44
ndn::security::transform::Downstream::getIndex
size_t getIndex() const
Get the module index.
Definition:
transform-base.hpp:125
ndn::security::transform::PublicKey
Abstraction of public key in crypto transformation.
Definition:
public-key.hpp:35
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
public-key.hpp
ndn::security::transform::VerifierFilter::Impl::Impl
Impl(const uint8_t *sig, size_t siglen)
Definition:
verifier-filter.cpp:35
ndn::security::transform::Transform::setOutputBuffer
void setOutputBuffer(unique_ptr< OBuffer > buffer)
Set output buffer to buffer.
Definition:
transform-base.cpp:104
ndn::security::transform::VerifierFilter::Impl::sig
const uint8_t * sig
Definition:
verifier-filter.cpp:43
ndn::security::detail::digestAlgorithmToEvpMd
const EVP_MD * digestAlgorithmToEvpMd(DigestAlgorithm algo)
Definition:
openssl-helper.cpp:29
ndn::security::transform::VerifierFilter::VerifierFilter
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.
Definition:
verifier-filter.cpp:48
ndn::security::transform::Error
Base class of transformation error.
Definition:
transform-base.hpp:47
transform
ndn::security::transform::verifierFilter
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
Definition:
verifier-filter.cpp:94
ndn::security::transform::VerifierFilter::Impl::ctx
detail::EvpMdCtx ctx
Definition:
verifier-filter.cpp:42
ndn::security::transform::VerifierFilter::Impl
Definition:
verifier-filter.cpp:32
ndn::security::detail::EvpMdCtx
Definition:
openssl-helper.hpp:38
ndn::make_unique
unique_ptr< T > make_unique(Args &&... args)
Definition:
backports.hpp:96
ndn::DigestAlgorithm
DigestAlgorithm
Definition:
security-common.hpp:105
ndn::security::transform::VerifierFilter::~VerifierFilter
~VerifierFilter()
ndnSIM
ndn-cxx
src
security
transform
verifier-filter.cpp
Generated on Tue Aug 7 2018 16:19:17 for ndnSIM by
1.8.14