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-2020 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"
26 #include "ndn-cxx/interest.hpp"
28 #include "ndn-cxx/security/impl/openssl.hpp"
37 
38 namespace ndn {
39 namespace security {
40 
41 namespace {
42 
43 class ParseResult
44 {
45 public:
46  ParseResult() = default;
47 
48  ParseResult(InputBuffers bufs, const uint8_t* sig, size_t sigLen)
49  : bufs(std::move(bufs))
50  , sig(sig)
51  , sigLen(sigLen)
52  {
53  }
54 
55 public:
56  InputBuffers bufs;
57  const uint8_t* sig = nullptr;
58  size_t sigLen = 0;
59 };
60 
61 } // namespace
62 
63 bool
64 verifySignature(const InputBuffers& blobs, const uint8_t* sig, size_t sigLen,
65  const transform::PublicKey& key)
66 {
67  bool result = false;
68  try {
69  using namespace transform;
70  bufferSource(blobs) >> verifierFilter(DigestAlgorithm::SHA256, key, sig, sigLen)
71  >> boolSink(result);
72  }
73  catch (const transform::Error&) {
74  return false;
75  }
76 
77  return result;
78 }
79 
80 bool
81 verifySignature(const uint8_t* blob, size_t blobLen, const uint8_t* sig, size_t sigLen,
82  const transform::PublicKey& key)
83 {
84  return verifySignature({{blob, blobLen}}, sig, sigLen, key);
85 }
86 
87 bool
88 verifySignature(const InputBuffers& blobs, const uint8_t* sig, size_t sigLen,
89  const uint8_t* key, size_t keyLen)
90 {
92  try {
93  pKey.loadPkcs8(key, keyLen);
94  }
95  catch (const transform::Error&) {
96  return false;
97  }
98 
99  return verifySignature(blobs, sig, sigLen, pKey);
100 }
101 
102 bool
103 verifySignature(const uint8_t* blob, size_t blobLen, const uint8_t* sig, size_t sigLen,
104  const uint8_t* key, size_t keyLen)
105 {
106  return verifySignature({{blob, blobLen}}, sig, sigLen, key, keyLen);
107 }
108 
109 static ParseResult
110 parse(const Data& data)
111 {
112  try {
113  return ParseResult(data.extractSignedRanges(),
114  data.getSignatureValue().value(),
115  data.getSignatureValue().value_size());
116  }
117  catch (const tlv::Error&) {
118  return ParseResult();
119  }
120 }
121 
122 static ParseResult
123 parse(const Interest& interest)
124 {
125  try {
126  interest.wireEncode();
127 
128  if (interest.getSignatureInfo() && interest.getSignatureValue().isValid()) {
129  // Verify using v0.3 Signed Interest semantics
130  Block sigValue = interest.getSignatureValue();
131  return ParseResult(interest.extractSignedRanges(),
132  sigValue.value(),
133  sigValue.value_size());
134  }
135  else {
136  // Verify using older Signed Interest semantics
137  const Name& interestName = interest.getName();
138  if (interestName.size() < signed_interest::MIN_SIZE) {
139  return ParseResult();
140  }
141 
142  const Block& nameBlock = interestName.wireEncode();
143  Block sigValue = interestName[signed_interest::POS_SIG_VALUE].blockFromValue();
144  return ParseResult({{nameBlock.value(),
145  nameBlock.value_size() - interestName[signed_interest::POS_SIG_VALUE].size()}},
146  sigValue.value(),
147  sigValue.value_size());
148  }
149  }
150  catch (const tlv::Error&) {
151  return ParseResult();
152  }
153 }
154 
155 static bool
156 verifySignature(ParseResult params, const transform::PublicKey& key)
157 {
158  return !params.bufs.empty() && verifySignature(params.bufs, params.sig, params.sigLen, key);
159 }
160 
161 static bool
162 verifySignature(ParseResult params, const tpm::Tpm& tpm, const Name& keyName,
163  DigestAlgorithm digestAlgorithm)
164 {
165  return !params.bufs.empty() && bool(tpm.verify(params.bufs, params.sig, params.sigLen, keyName, digestAlgorithm));
166 }
167 
168 static bool
169 verifySignature(ParseResult params, const uint8_t* key, size_t keyLen)
170 {
171  return !params.bufs.empty() && verifySignature(params.bufs, params.sig, params.sigLen, key, keyLen);
172 }
173 
174 bool
175 verifySignature(const Data& data, const transform::PublicKey& key)
176 {
177  return verifySignature(parse(data), key);
178 }
179 
180 bool
181 verifySignature(const Interest& interest, const transform::PublicKey& key)
182 {
183  return verifySignature(parse(interest), key);
184 }
185 
186 bool
187 verifySignature(const Data& data, const pib::Key& key)
188 {
189  return verifySignature(parse(data), key.getPublicKey().data(), key.getPublicKey().size());
190 }
191 
192 bool
193 verifySignature(const Interest& interest, const pib::Key& key)
194 {
195  return verifySignature(parse(interest), key.getPublicKey().data(), key.getPublicKey().size());
196 }
197 
198 bool
199 verifySignature(const Data& data, const uint8_t* key, size_t keyLen)
200 {
201  return verifySignature(parse(data), key, keyLen);
202 }
203 
204 bool
205 verifySignature(const Interest& interest, const uint8_t* key, size_t keyLen)
206 {
207  return verifySignature(parse(interest), key, keyLen);
208 }
209 
210 bool
211 verifySignature(const Data& data, const v2::Certificate& cert)
212 {
213  return verifySignature(parse(data), cert.getContent().value(), cert.getContent().value_size());
214 }
215 
216 bool
217 verifySignature(const Interest& interest, const v2::Certificate& cert)
218 {
219  return verifySignature(parse(interest), cert.getContent().value(), cert.getContent().value_size());
220 }
221 
222 bool
223 verifySignature(const Data& data, const tpm::Tpm& tpm,
224  const Name& keyName, DigestAlgorithm digestAlgorithm)
225 {
226  return verifySignature(parse(data), tpm, keyName, digestAlgorithm);
227 }
228 
229 bool
230 verifySignature(const Interest& interest, const tpm::Tpm& tpm,
231  const Name& keyName, DigestAlgorithm digestAlgorithm)
232 {
233  return verifySignature(parse(interest), tpm, keyName, digestAlgorithm);
234 }
235 
237 
238 bool
239 verifyDigest(const InputBuffers& bufs, const uint8_t* digest, size_t digestLen,
240  DigestAlgorithm algorithm)
241 {
242  using namespace transform;
243 
244  OBufferStream os;
245  try {
246  bufferSource(bufs) >> digestFilter(algorithm) >> streamSink(os);
247  }
248  catch (const transform::Error&) {
249  return false;
250  }
251  ConstBufferPtr result = os.buf();
252 
253  if (result->size() != digestLen) {
254  return false;
255  }
256 
257  // constant-time buffer comparison to mitigate timing attacks
258  return CRYPTO_memcmp(result->data(), digest, digestLen) == 0;
259 }
260 
261 bool
262 verifyDigest(const uint8_t* blob, size_t blobLen, const uint8_t* digest, size_t digestLen,
263  DigestAlgorithm algorithm)
264 {
265  return verifyDigest({{blob, blobLen}}, digest, digestLen, algorithm);
266 }
267 
268 bool
269 verifyDigest(const Data& data, DigestAlgorithm algorithm)
270 {
271  ParseResult parseResult = parse(data);
272  return !parseResult.bufs.empty() && verifyDigest(parseResult.bufs, parseResult.sig,
273  parseResult.sigLen, algorithm);
274 }
275 
276 bool
277 verifyDigest(const Interest& interest, DigestAlgorithm algorithm)
278 {
279  ParseResult parseResult = parse(interest);
280  return !parseResult.bufs.empty() && verifyDigest(parseResult.bufs, parseResult.sig,
281  parseResult.sigLen, algorithm);
282 }
283 
284 } // namespace security
285 } // namespace ndn
Definition: data.cpp:26
The certificate following the certificate format naming convention.
Definition: certificate.hpp:81
STL namespace.
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
const size_t MIN_SIZE
minimal number of components for Signed Interest
InputBuffers extractSignedRanges() const
Extract ranges of Interest covered by the signature in Packet Specification v0.3. ...
Definition: interest.cpp:640
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
Represents an Interest packet.
Definition: interest.hpp:50
Abstraction of public key in crypto transformation.
Definition: public-key.hpp:35
const Block & getSignatureValue() const noexcept
Get SignatureValue.
Definition: data.hpp:251
TPM front-end class.
Definition: tpm.hpp:65
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
Definition: interest.cpp:546
boost::logic::tribool verify(const InputBuffers &bufs, const uint8_t *sig, size_t sigLen, const Name &keyName, DigestAlgorithm digestAlgorithm) const
Verify discontiguous ranges using the key with name keyName and using the digest digestAlgorithm.
Definition: tpm.cpp:97
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:330
A frontend handle of a key instance.
Definition: key.hpp:49
bool verifyDigest(const InputBuffers &bufs, const uint8_t *digest, size_t digestLen, DigestAlgorithm algorithm)
Verify blobs against digest using algorithm.
size_t sigLen
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
const uint8_t * sig
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
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
Use the SHA-256 hash of the public key as key id.
Represents an absolute name.
Definition: name.hpp:44
Base class of transformation error.
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, const uint8_t *sig, size_t sigLen)
const ssize_t POS_SIG_VALUE
bool verifySignature(const InputBuffers &blobs, const uint8_t *sig, size_t sigLen, const transform::PublicKey &key)
Verify blobs using key against sig.
size_t size() const
Returns the number of components.
Definition: name.hpp:154
Block getSignatureValue() const
Get the InterestSignatureValue.
Definition: interest.cpp:590
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition: interest.cpp:89
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:188
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:172
const Name & getName() const noexcept
Definition: interest.hpp:174
const Buffer & getPublicKey() const
Get public key bits.
Definition: key.cpp:56
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:117
implements an output stream that constructs ndn::Buffer
static ParseResult parse(const Data &data)
InputBuffers bufs
Represents a Data packet.
Definition: data.hpp:39
unique_ptr< Sink > boolSink(bool &value)
Definition: bool-sink.cpp:51
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126