ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2024 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
24
25namespace ndn::security {
26
27void
28InterestSigner::makeSignedInterest(Interest& interest, SigningInfo params, uint32_t signingFlags)
29{
31
32 if ((signingFlags & (WantNonce | WantTime | WantSeqNum)) == 0) {
33 NDN_THROW(std::invalid_argument("No signature elements specified"));
34 }
35
36 if (signingFlags & WantNonce) {
37 std::vector<uint8_t> nonce(8);
39 info.setNonce(nonce);
40 }
41
42 if (signingFlags & WantTime) {
43 info.setTime(getFreshTimestamp());
44 }
45
46 if (signingFlags & WantSeqNum) {
47 info.setSeqNum(++m_lastUsedSeqNum);
48 }
49
50 params.setSignatureInfo(info);
52 m_keyChain.sign(interest, params);
53}
54
57{
58 Interest interest;
59 time::milliseconds timestamp = time::toUnixTimestamp(getFreshTimestamp());
60 name
61 .append(name::Component::fromNumber(timestamp.count()))
63 ;
64 interest.setName(name);
65 m_keyChain.sign(interest, params);
66 return interest;
67}
68
70InterestSigner::getFreshTimestamp()
71{
72 auto timestamp = time::system_clock::now();
73 if (time::duration_cast<time::milliseconds>(timestamp - m_lastUsedTimestamp) > 0_ms) {
74 m_lastUsedTimestamp = timestamp;
75 }
76 else {
77 m_lastUsedTimestamp = m_lastUsedTimestamp + 1_ms;
78 timestamp = m_lastUsedTimestamp;
79 }
80 return timestamp;
81}
82
83} // namespace ndn::security
Represents an Interest packet.
Definition interest.hpp:50
Interest & setName(const Name &name)
Set the Interest name.
Definition interest.cpp:354
Represents an absolute name.
Definition name.hpp:45
Name & append(const Component &component)
Append a name component.
Definition name.hpp:308
Represents a SignatureInfo or InterestSignatureInfo TLV element.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
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)
void sign(Data &data, const SigningInfo &params=SigningInfo())
Sign a Data packet according to the supplied signing information.
Signing parameters passed to KeyChain.
SigningInfo & setSignatureInfo(const SignatureInfo &signatureInfo)
Set a semi-prepared SignatureInfo.
const SignatureInfo & getSignatureInfo() const
Get a semi-prepared SignatureInfo.
SigningInfo & setSignedInterestFormat(SignedInterestFormat signedInterestFormat)
Set the signed Interest format.
static time_point now() noexcept
Definition time.cpp:45
::boost::chrono::time_point< system_clock > time_point
Definition time.hpp:205
#define NDN_THROW(e)
Definition exception.hpp:56
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
Contains the ndn-cxx security framework.
@ V03
Sign Interest using Packet Specification v0.3 semantics.
constexpr Duration toUnixTimestamp(const system_clock::time_point &tp)
Convert system_clock::time_point to UNIX timestamp.
Definition time.hpp:265
::boost::chrono::milliseconds milliseconds
Definition time.hpp:52
SignatureInfo info