NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
interest-signer.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 #include "ndn-cxx/util/random.hpp"
24 
25 namespace ndn {
26 namespace security {
27 
29  : m_keyChain(keyChain)
30  , m_lastUsedSeqNum(-1) // Will wrap around to 0 on next Interest
31 {
32 }
33 
34 void
35 InterestSigner::makeSignedInterest(Interest& interest, SigningInfo params, uint32_t signingFlags)
36 {
38 
39  if ((signingFlags & (WantNonce | WantTime | WantSeqNum)) == 0) {
40  NDN_THROW(std::invalid_argument("No signature elements specified"));
41  }
42 
43  if (signingFlags & WantNonce) {
44  std::vector<uint8_t> nonce(8);
46  info.setNonce(nonce);
47  }
48 
49  if (signingFlags & WantTime) {
50  info.setTime(getFreshTimestamp());
51  }
52 
53  if (signingFlags & WantSeqNum) {
54  info.setSeqNum(++m_lastUsedSeqNum);
55  }
56 
57  params.setSignatureInfo(info);
59  m_keyChain.sign(interest, params);
60 }
61 
64 {
65  Interest interest;
66  time::milliseconds timestamp = time::toUnixTimestamp(getFreshTimestamp());
67  name
68  .append(name::Component::fromNumber(timestamp.count()))
70  ;
71  interest.setName(name);
72  m_keyChain.sign(interest, params);
73  return interest;
74 }
75 
77 InterestSigner::getFreshTimestamp()
78 {
79  auto timestamp = time::system_clock::now();
80  if (time::duration_cast<time::milliseconds>(timestamp - m_lastUsedTimestamp) > 0_ms) {
81  m_lastUsedTimestamp = timestamp;
82  }
83  else {
84  m_lastUsedTimestamp = m_lastUsedTimestamp + 1_ms;
85  timestamp = m_lastUsedTimestamp;
86  }
87  return timestamp;
88 }
89 
90 } // namespace security
91 } // namespace ndn
SignatureInfo & setSeqNum(optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
Sign Interest using Packet Specification v0.3 semantics.
Copyright (c) 2011-2015 Regents of the University of California.
Represents a SignatureInfo or InterestSignatureInfo TLV element.
const SignatureInfo & getSignatureInfo() const
ndn security KeyChain
Definition: key-chain.cpp:70
Interest makeCommandInterest(Name name, const SigningInfo &params=SigningInfo())
Creates and signs a command Interest.
InterestSigner(KeyChain &keyChain)
SignatureInfo info
Represents an Interest packet.
Definition: interest.hpp:48
static time_point now() noexcept
Definition: time.cpp:46
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.cpp:113
Name & append(const Component &component)
Append a component.
Definition: name.hpp:275
void generateSecureBytes(span< uint8_t > buf)
Fill buffer with cryptographically secure random bytes.
Definition: random.cpp:45
Signing parameters passed to KeyChain.
#define NDN_THROW(e)
Definition: exception.hpp:61
SignatureInfo & setTime(optional< time::system_clock::time_point > time=time::system_clock::now())
Append or replace SignatureTime.
Represents an absolute name.
Definition: name.hpp:41
SignatureInfo & setNonce(optional< span< const uint8_t >> nonce)
Append or replace SignatureNonce.
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
uint64_t generateWord64()
Generate a non-cryptographically-secure random integer in the range [0, 2^64)
Definition: random.cpp:73
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
void makeSignedInterest(Interest &interest, SigningInfo params=SigningInfo(), uint32_t signingFlags=WantNonce|WantTime)
Signs an Interest (following Packet Specification v0.3 or newer)
SigningInfo & setSignedInterestFormat(SignedInterestFormat signedInterestFormat)
Set signed Interest format.
time_point TimePoint
Definition: time.hpp:203
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
Interest & setName(const Name &name)
Set the Interest&#39;s name.
Definition: interest.cpp:367