NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
verification-helpers.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 
23 
24 #include "ndn-cxx/data.hpp"
25 #include "ndn-cxx/interest.hpp"
38 
39 namespace ndn {
40 namespace security {
41 
42 namespace {
43 
44 struct ParseResult
45 {
46  bool isParsable = false;
47  const uint8_t* buf = nullptr;
48  size_t bufLen = 0;
49  const uint8_t* sig = nullptr;
50  size_t sigLen = 0;
51 };
52 
53 } // namespace
54 
55 bool
56 verifySignature(const uint8_t* blob, size_t blobLen, const uint8_t* sig, size_t sigLen,
57  const transform::PublicKey& key)
58 {
59  bool result = false;
60  try {
61  using namespace transform;
63  >> boolSink(result);
64  }
65  catch (const transform::Error&) {
66  return false;
67  }
68  return result;
69 }
70 
71 bool
72 verifySignature(const uint8_t* data, size_t dataLen, const uint8_t* sig, size_t sigLen,
73  const uint8_t* key, size_t keyLen)
74 {
76  try {
77  pKey.loadPkcs8(key, keyLen);
78  }
79  catch (const transform::Error&) {
80  return false;
81  }
82 
83  return verifySignature(data, dataLen, sig, sigLen, pKey);
84 }
85 
86 static ParseResult
87 parse(const Data& data)
88 {
89  try {
90  return {true,
91  data.wireEncode().value(),
92  data.wireEncode().value_size() - data.getSignature().getValue().size(),
93  data.getSignature().getValue().value(),
94  data.getSignature().getValue().value_size()};
95  }
96  catch (const tlv::Error&) {
97  return ParseResult();
98  }
99 }
100 
101 static ParseResult
102 parse(const Interest& interest)
103 {
104  const Name& interestName = interest.getName();
105 
106  if (interestName.size() < signed_interest::MIN_SIZE)
107  return ParseResult();
108 
109  try {
110  const Block& nameBlock = interestName.wireEncode();
111  return {true,
112  nameBlock.value(),
113  nameBlock.value_size() - interestName[signed_interest::POS_SIG_VALUE].size(),
114  interestName[signed_interest::POS_SIG_VALUE].blockFromValue().value(),
115  interestName[signed_interest::POS_SIG_VALUE].blockFromValue().value_size()};
116  }
117  catch (const tlv::Error&) {
118  return ParseResult();
119  }
120 }
121 
122 static bool
123 verifySignature(ParseResult params, const transform::PublicKey& key)
124 {
125  return params.isParsable && verifySignature(params.buf, params.bufLen,
126  params.sig, params.sigLen, key);
127 }
128 
129 static bool
130 verifySignature(ParseResult params, const tpm::Tpm& tpm, const Name& keyName,
131  DigestAlgorithm digestAlgorithm)
132 {
133  return params.isParsable && bool(tpm.verify(params.buf, params.bufLen,
134  params.sig, params.sigLen, keyName, digestAlgorithm));
135 }
136 
137 static bool
138 verifySignature(ParseResult params, const uint8_t* key, size_t keyLen)
139 {
140  return params.isParsable && verifySignature(params.buf, params.bufLen,
141  params.sig, params.sigLen, key, keyLen);
142 }
143 
144 bool
145 verifySignature(const Data& data, const transform::PublicKey& key)
146 {
147  return verifySignature(parse(data), key);
148 }
149 
150 bool
151 verifySignature(const Interest& interest, const transform::PublicKey& key)
152 {
153  return verifySignature(parse(interest), key);
154 }
155 
156 bool
157 verifySignature(const Data& data, const pib::Key& key)
158 {
159  return verifySignature(parse(data), key.getPublicKey().data(), key.getPublicKey().size());
160 }
161 
162 bool
163 verifySignature(const Interest& interest, const pib::Key& key)
164 {
165  return verifySignature(parse(interest), key.getPublicKey().data(), key.getPublicKey().size());
166 }
167 
168 bool
169 verifySignature(const Data& data, const uint8_t* key, size_t keyLen)
170 {
171  return verifySignature(parse(data), key, keyLen);
172 }
173 
174 bool
175 verifySignature(const Interest& interest, const uint8_t* key, size_t keyLen)
176 {
177  return verifySignature(parse(interest), key, keyLen);
178 }
179 
180 bool
181 verifySignature(const Data& data, const v2::Certificate& cert)
182 {
183  return verifySignature(parse(data), cert.getContent().value(), cert.getContent().value_size());
184 }
185 
186 bool
187 verifySignature(const Interest& interest, const v2::Certificate& cert)
188 {
189  return verifySignature(parse(interest), cert.getContent().value(), cert.getContent().value_size());
190 }
191 
192 bool
193 verifySignature(const Data& data, const tpm::Tpm& tpm,
194  const Name& keyName, DigestAlgorithm digestAlgorithm)
195 {
196  return verifySignature(parse(data), tpm, keyName, digestAlgorithm);
197 }
198 
199 bool
200 verifySignature(const Interest& interest, const tpm::Tpm& tpm,
201  const Name& keyName, DigestAlgorithm digestAlgorithm)
202 {
203  return verifySignature(parse(interest), tpm, keyName, digestAlgorithm);
204 }
205 
207 
208 bool
209 verifyDigest(const uint8_t* blob, size_t blobLen, const uint8_t* digest, size_t digestLen,
210  DigestAlgorithm algorithm)
211 {
212  using namespace transform;
213 
214  OBufferStream os;
215  try {
216  bufferSource(blob, blobLen) >> digestFilter(algorithm) >> streamSink(os);
217  }
218  catch (const transform::Error&) {
219  return false;
220  }
221  ConstBufferPtr result = os.buf();
222 
223  if (result->size() != digestLen)
224  return false;
225 
226  // constant-time buffer comparison to mitigate timing attacks
227  return CRYPTO_memcmp(result->data(), digest, digestLen) == 0;
228 }
229 
230 bool
231 verifyDigest(const Data& data, DigestAlgorithm algorithm)
232 {
233  ParseResult parseResult = parse(data);
234  return parseResult.isParsable && verifyDigest(parseResult.buf, parseResult.bufLen,
235  parseResult.sig, parseResult.sigLen, algorithm);
236 }
237 
238 bool
239 verifyDigest(const Interest& interest, DigestAlgorithm algorithm)
240 {
241  ParseResult parseResult = parse(interest);
242  return parseResult.isParsable && verifyDigest(parseResult.buf, parseResult.bufLen,
243  parseResult.sig, parseResult.sigLen, algorithm);
244 }
245 
246 } // namespace security
247 } // namespace ndn
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
key.hpp
ndn::security::verifyDigest
bool verifyDigest(const uint8_t *blob, size_t blobLen, const uint8_t *digest, size_t digestLen, DigestAlgorithm algorithm)
Verify blob against digest using algorithm.
Definition: verification-helpers.cpp:209
isParsable
bool isParsable
Definition: verification-helpers.cpp:46
ndn::Signature::getValue
const Block & getValue() const
Get SignatureValue.
Definition: signature.hpp:95
buffer-source.hpp
bufLen
size_t bufLen
Definition: verification-helpers.cpp:48
transform
certificate.hpp
sigLen
size_t sigLen
Definition: verification-helpers.cpp:50
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
ndn::security::transform::PublicKey::loadPkcs8
void loadPkcs8(const uint8_t *buf, size_t size)
Load the public key in PKCS#8 format from a buffer buf.
Definition: public-key.cpp:88
verifier-filter.hpp
bool-sink.hpp
ndn::Name::size
size_t size() const
Returns the number of components.
Definition: name.hpp:153
ndn::DigestAlgorithm::SHA256
@ SHA256
openssl.hpp
public-key.hpp
ndn::Data::getContent
const Block & getContent() const
Get Content.
Definition: data.cpp:232
ndn::security::transform::bufferSource
BufferSource bufferSource
Definition: buffer-source.hpp:73
ndn::Data::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder, bool wantUnsignedPortionOnly=false) const
Prepend wire encoding to encoder in NDN Packet Format v0.2.
Definition: data.cpp:48
ndn::security::verifySignature
bool verifySignature(const uint8_t *blob, size_t blobLen, const uint8_t *sig, size_t sigLen, const transform::PublicKey &key)
Verify blob using key against sig.
Definition: verification-helpers.cpp:56
ndn::DigestAlgorithm
DigestAlgorithm
Definition: security-common.hpp:96
ndn::security::transform::Error
Base class of transformation error.
Definition: transform-base.hpp:49
ndn::Name
Represents an absolute name.
Definition: name.hpp:44
digest-filter.hpp
ndn::security::v2::Certificate
The certificate following the certificate format naming convention.
Definition: certificate.hpp:82
key-handle.hpp
ndn::signed_interest::POS_SIG_VALUE
const ssize_t POS_SIG_VALUE
Definition: security-common.hpp:31
ndn::Name::wireEncode
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
ndn::security::tpm::Tpm::verify
boost::logic::tribool verify(const uint8_t *buf, size_t bufLen, const uint8_t *sig, size_t sigLen, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Verify blob using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.cpp:95
stream-sink.hpp
verification-helpers.hpp
sig
const uint8_t * sig
Definition: verification-helpers.cpp:49
ndn::Interest
Represents an Interest packet.
Definition: interest.hpp:44
ndn::security::tpm::Tpm
TPM front-end class.
Definition: tpm.hpp:66
tpm.hpp
ndn::Data
Represents a Data packet.
Definition: data.hpp:36
ndn::security::parse
static ParseResult parse(const Data &data)
Definition: verification-helpers.cpp:87
ndn::OBufferStream
implements an output stream that constructs ndn::Buffer
Definition: buffer-stream.hpp:71
interest.hpp
ndn::security::transform::digestFilter
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
Definition: digest-filter.cpp:76
data.hpp
ndn::security::transform::PublicKey
Abstraction of public key in crypto transformation.
Definition: public-key.hpp:36
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::security::pib::Key
A frontend handle of a key instance.
Definition: key.hpp:50
ndn::security::transform::streamSink
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
ndn::signed_interest::MIN_SIZE
const size_t MIN_SIZE
minimal number of components for Signed Interest
Definition: security-common.hpp:37
ndn::Interest::getName
const Name & getName() const noexcept
Definition: interest.hpp:121
ndn::security::pib::Key::getPublicKey
const Buffer & getPublicKey() const
Get public key bits.
Definition: key.cpp:56
buffer-stream.hpp
ndn::security::transform::verifierFilter
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
Definition: verifier-filter.cpp:130
ndn::security::transform::boolSink
unique_ptr< Sink > boolSink(bool &value)
Definition: bool-sink.cpp:51
ndn::OBufferStream::buf
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer-stream.cpp:54
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::Data::getSignature
const Signature & getSignature() const
Get Signature.
Definition: data.hpp:185