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-2022 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(SignatureInfo info, InputBuffers bufs, span<const uint8_t> sig)
49  : info(std::move(info))
50  , bufs(std::move(bufs))
51  , sig(sig)
52  {
53  }
54 
55 public:
57  InputBuffers bufs;
58  span<const uint8_t> sig;
59 };
60 
61 } // namespace
62 
63 bool
64 verifySignature(const InputBuffers& blobs, span<const uint8_t> sig, const transform::PublicKey& key)
65 {
66  bool result = false;
67  try {
68  using namespace transform;
70  >> boolSink(result);
71  }
72  catch (const transform::Error&) {
73  return false;
74  }
75 
76  return result;
77 }
78 
79 bool
80 verifySignature(const InputBuffers& blobs, span<const uint8_t> sig, span<const uint8_t> key)
81 {
83  try {
84  pKey.loadPkcs8(key);
85  }
86  catch (const transform::Error&) {
87  return false;
88  }
89 
90  return verifySignature(blobs, sig, pKey);
91 }
92 
93 static ParseResult
94 parse(const Data& data)
95 {
96  try {
97  return ParseResult(data.getSignatureInfo(),
98  data.extractSignedRanges(),
99  {data.getSignatureValue().value(), data.getSignatureValue().value_size()});
100  }
101  catch (const tlv::Error&) {
102  return ParseResult();
103  }
104 }
105 
106 static ParseResult
107 parse(const Interest& interest)
108 {
109  try {
110  interest.wireEncode();
111 
112  if (interest.getSignatureInfo() && interest.getSignatureValue().isValid()) {
113  // Verify using v0.3 Signed Interest semantics
114  Block sigValue = interest.getSignatureValue();
115  return ParseResult(*interest.getSignatureInfo(),
116  interest.extractSignedRanges(),
117  {sigValue.value(), sigValue.value_size()});
118  }
119  else {
120  // Verify using older Signed Interest semantics
121  const Name& interestName = interest.getName();
122  if (interestName.size() < signed_interest::MIN_SIZE) {
123  return ParseResult();
124  }
125 
126  const Block& nameBlock = interestName.wireEncode();
127  SignatureInfo info(interestName[signed_interest::POS_SIG_INFO].blockFromValue());
128  Block sigValue(interestName[signed_interest::POS_SIG_VALUE].blockFromValue());
129  return ParseResult(info,
130  {{nameBlock.value(),
131  nameBlock.value_size() - interestName[signed_interest::POS_SIG_VALUE].size()}},
132  {sigValue.value(),
133  sigValue.value_size()});
134  }
135  }
136  catch (const tlv::Error&) {
137  return ParseResult();
138  }
139 }
140 
141 static bool
142 verifySignature(const ParseResult& params, const transform::PublicKey& key)
143 {
144  return !params.bufs.empty() && verifySignature(params.bufs, params.sig, key);
145 }
146 
147 static bool
148 verifySignature(const ParseResult& params, span<const uint8_t> key)
149 {
150  return !params.bufs.empty() && verifySignature(params.bufs, params.sig, key);
151 }
152 
153 static bool
154 verifySignature(const ParseResult& params, const tpm::Tpm& tpm, const Name& keyName,
155  DigestAlgorithm digestAlgorithm)
156 {
157  return !params.bufs.empty() && bool(tpm.verify(params.bufs, params.sig, keyName, digestAlgorithm));
158 }
159 
160 static bool
161 verifyDigest(const ParseResult& params, DigestAlgorithm algorithm)
162 {
163  if (params.bufs.empty()) {
164  return false;
165  }
166 
167  OBufferStream os;
168  try {
169  using namespace transform;
170  bufferSource(params.bufs) >> digestFilter(algorithm) >> streamSink(os);
171  }
172  catch (const transform::Error&) {
173  return false;
174  }
175  auto result = os.buf();
176 
177  if (result->size() != params.sig.size()) {
178  return false;
179  }
180 
181  // constant-time buffer comparison to mitigate timing attacks
182  return CRYPTO_memcmp(result->data(), params.sig.data(), params.sig.size()) == 0;
183 }
184 
185 bool
186 verifySignature(const Data& data, span<const uint8_t> key)
187 {
188  return verifySignature(parse(data), key);
189 }
190 
191 bool
192 verifySignature(const Interest& interest, span<const uint8_t> key)
193 {
194  return verifySignature(parse(interest), key);
195 }
196 
197 bool
198 verifySignature(const Data& data, const transform::PublicKey& key)
199 {
200  return verifySignature(parse(data), key);
201 }
202 
203 bool
204 verifySignature(const Interest& interest, const transform::PublicKey& key)
205 {
206  return verifySignature(parse(interest), key);
207 }
208 
209 bool
210 verifySignature(const Data& data, const pib::Key& key)
211 {
212  return verifySignature(parse(data), key.getPublicKey());
213 }
214 
215 bool
216 verifySignature(const Interest& interest, const pib::Key& key)
217 {
218  return verifySignature(parse(interest), key.getPublicKey());
219 }
220 
221 bool
222 verifySignature(const Data& data, const optional<Certificate>& cert)
223 {
224  auto parsed = parse(data);
225  if (cert) {
226  return verifySignature(parsed, {cert->getContent().value(), cert->getContent().value_size()});
227  }
228  else if (parsed.info.getSignatureType() == tlv::SignatureTypeValue::DigestSha256) {
229  return verifyDigest(parsed, DigestAlgorithm::SHA256);
230  }
231  // Add any other self-verifying signatures here (if any)
232  else {
233  return false;
234  }
235 }
236 
237 bool
238 verifySignature(const Interest& interest, const optional<Certificate>& cert)
239 {
240  auto parsed = parse(interest);
241  if (cert) {
242  return verifySignature(parsed, {cert->getContent().value(), cert->getContent().value_size()});
243  }
244  else if (parsed.info.getSignatureType() == tlv::SignatureTypeValue::DigestSha256) {
245  return verifyDigest(parsed, DigestAlgorithm::SHA256);
246  }
247  // Add any other self-verifying signatures here (if any)
248  else {
249  return false;
250  }
251 }
252 
253 bool
254 verifySignature(const Data& data, const tpm::Tpm& tpm,
255  const Name& keyName, DigestAlgorithm digestAlgorithm)
256 {
257  return verifySignature(parse(data), tpm, keyName, digestAlgorithm);
258 }
259 
260 bool
261 verifySignature(const Interest& interest, const tpm::Tpm& tpm,
262  const Name& keyName, DigestAlgorithm digestAlgorithm)
263 {
264  return verifySignature(parse(interest), tpm, keyName, digestAlgorithm);
265 }
266 
267 } // namespace security
268 } // namespace ndn
Represents a Data packet.
Definition: data.hpp:38
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:322
const SignatureInfo & getSignatureInfo() const noexcept
Get SignatureInfo.
Definition: data.hpp:227
Represents an Interest packet.
Definition: interest.hpp:50
Represents an absolute name.
Definition: name.hpp:46
A frontend handle of a key instance.
Definition: key.hpp:50
span< const uint8_t > getPublicKey() const
Get public key bits.
Definition: key.cpp:56
TPM front-end class.
Definition: tpm.hpp:66
Base class of transformation error.
Abstraction of public key in crypto transformation.
Definition: public-key.hpp:36
void loadPkcs8(span< const uint8_t > buf)
Load the public key in PKCS#8 format from a buffer buf.
Definition: public-key.cpp:88
unique_ptr< Transform > digestFilter(DigestAlgorithm algo)
unique_ptr< Sink > streamSink(std::ostream &os)
Definition: stream-sink.cpp:53
unique_ptr< Sink > boolSink(bool &value)
Definition: bool-sink.cpp:51
unique_ptr< Transform > verifierFilter(DigestAlgorithm algo, const PublicKey &key, span< const uint8_t > sig)
bool verifySignature(const InputBuffers &blobs, span< const uint8_t > sig, const transform::PublicKey &key)
Verify blobs using key against sig.
const ssize_t POS_SIG_VALUE
const size_t MIN_SIZE
minimal number of components for Signed Interest
const ssize_t POS_SIG_INFO
@ Name
Definition: tlv.hpp:67
@ SignatureInfo
Definition: tlv.hpp:82
@ Interest
Definition: tlv.hpp:65
@ DigestSha256
Definition: tlv.hpp:133
Definition: data.cpp:25
InputBuffers bufs
span< const uint8_t > sig
SignatureInfo info