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-2021 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  interest.setCanBePrefix(false);
73  m_keyChain.sign(interest, params);
74  return interest;
75 }
76 
78 InterestSigner::getFreshTimestamp()
79 {
80  auto timestamp = time::system_clock::now();
81  if (time::duration_cast<time::milliseconds>(timestamp - m_lastUsedTimestamp) > 0_ms) {
82  m_lastUsedTimestamp = timestamp;
83  }
84  else {
85  m_lastUsedTimestamp = m_lastUsedTimestamp + 1_ms;
86  timestamp = m_lastUsedTimestamp;
87  }
88  return timestamp;
89 }
90 
91 } // namespace security
92 } // namespace ndn
Represents an Interest packet.
Definition: interest.hpp:50
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:216
Interest & setName(const Name &name)
Set the Interest's name.
Definition: interest.cpp:372
Represents an absolute name.
Definition: name.hpp:46
Name & append(const Component &component)
Append a component.
Definition: name.hpp:279
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 signed Interest format.
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
const SignatureInfo & getSignatureInfo() const
The interface of signing key management.
Definition: key-chain.hpp:46
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:442
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:45
uint64_t generateWord64()
Generate a non-cryptographically-secure random integer in the range [0, 2^64)
Definition: random.cpp:73
@ 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