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-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 #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 {
37  SignatureInfo info = params.getSignatureInfo();
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);
45  random::generateSecureBytes(nonce.data(), nonce.size());
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
SignatureInfo & setSeqNum(optional< uint64_t > seqNum)
Append or replace SignatureSeqNum.
Sign Interest using Packet Specification v0.3 semantics.
Definition: data.cpp:26
Represents a SignatureInfo or InterestSignatureInfo TLV element.
The interface of signing key management.
Definition: key-chain.hpp:45
const SignatureInfo & getSignatureInfo() const
Interest makeCommandInterest(Name name, const SigningInfo &params=SigningInfo())
Creates and signs a command Interest.
InterestSigner(KeyChain &keyChain)
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Definition: key-chain.cpp:456
Represents an Interest packet.
Definition: interest.hpp:50
static time_point now() noexcept
Definition: time.cpp:46
Name & append(const Component &component)
Append a component.
Definition: name.hpp:278
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:44
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
time_point TimePoint
Definition: time.hpp:195
uint64_t generateWord64()
Generate a non-cryptographically-secure random integer in the range [0, 2^64)
Definition: random.cpp:73
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:113
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as nonNegativeInteger.
void generateSecureBytes(uint8_t *bytes, size_t size)
Fill bytes of size with cryptographically secure random bytes.
Definition: random.cpp:45
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.
SignatureInfo & setNonce(optional< std::vector< uint8_t >> nonce)
Append or replace SignatureNonce.
Interest & setCanBePrefix(bool canBePrefix)
Add or remove CanBePrefix element.
Definition: interest.hpp:217
Interest & setName(const Name &name)
Set the Interest&#39;s name.
Definition: interest.cpp:375