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
Represents an Interest packet.
Definition: interest.hpp:50
Interest & setName(const Name &name)
Set the Interest's name.
Definition: interest.cpp:369
Represents an absolute name.
Definition: name.hpp:44
Name & append(const Component &component)
Append a name component.
Definition: name.hpp:283
Represents a SignatureInfo or InterestSignatureInfo TLV element.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
InterestSigner(KeyChain &keyChain)
Interest makeCommandInterest(Name name, const SigningInfo &params=SigningInfo())
Creates and signs a command Interest.
void makeSignedInterest(Interest &interest, SigningInfo params=SigningInfo(), uint32_t signingFlags=WantNonce|WantTime)
Signs an Interest (following Packet Specification v0.3 or newer)
Signing parameters passed to KeyChain.
SigningInfo & setSignedInterestFormat(SignedInterestFormat signedInterestFormat)
Set the signed Interest format.
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
const SignatureInfo & getSignatureInfo() const
Get a semi-prepared SignatureInfo.
The main interface for signing key management.
Definition: key-chain.hpp:87
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:403
static time_point now() noexcept
Definition: time.cpp:46
time_point TimePoint
Definition: time.hpp:203
#define NDN_THROW(e)
Definition: exception.hpp:61
void generateSecureBytes(span< uint8_t > buf)
Fill buffer with cryptographically secure random bytes.
Definition: random.cpp:47
uint64_t generateWord64()
Generate a non-cryptographically-secure random integer in the range [0, 2^64)
Definition: random.cpp:75
@ V03
Sign Interest using Packet Specification v0.3 semantics.
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.cpp:113
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
Definition: data.cpp:25
SignatureInfo info