blob: e1d252acbf60db4ad956774389de80bd4d71fc75 [file] [log] [blame]
Eric Newberry17d7c472020-06-18 21:29:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento09904412021-03-24 16:40:53 -04003 * Copyright (c) 2013-2021 Regents of the University of California.
Eric Newberry17d7c472020-06-18 21:29:22 -07004 *
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
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_SECURITY_INTEREST_SIGNER_HPP
23#define NDN_CXX_SECURITY_INTEREST_SIGNER_HPP
Eric Newberry17d7c472020-06-18 21:29:22 -070024
25#include "ndn-cxx/security/key-chain.hpp"
26
27namespace ndn {
28namespace security {
29
30/**
31 * @brief Helper class to create signed Interests
32 *
Eric Newberry1caa6342020-08-23 19:29:08 -070033 * The signer generates signature elements for an Interest and signs it with the KeyChain.
Eric Newberry17d7c472020-06-18 21:29:22 -070034 */
35class InterestSigner
36{
37public:
Eric Newberry1caa6342020-08-23 19:29:08 -070038 /**
39 * @brief Flags to indicate which elements to include in Interest signatures created with
40 * makeSignedInterest.
41 * @sa https://named-data.net/doc/NDN-packet-spec/0.3/signature.html#interest-signature
42 */
43 enum SigningFlags : uint32_t {
44 WantNonce = 1 << 0,
45 WantTime = 1 << 1,
46 WantSeqNum = 1 << 2,
47 };
48
49public:
Eric Newberry17d7c472020-06-18 21:29:22 -070050 explicit
51 InterestSigner(KeyChain& keyChain);
52
53 /**
Eric Newberry1caa6342020-08-23 19:29:08 -070054 * @brief Signs an Interest (following Packet Specification v0.3 or newer)
55 * @param interest Interest to sign
56 * @param params SigningInfo that provides parameters on how to sign the Interest.
57 * @param signingFlags Indicates which elements to include in the signature. At least one element
58 * must be specified for inclusion.
59 * @throw std::invalid_argument No signature elements were specified for inclusion.
Eric Newberry17d7c472020-06-18 21:29:22 -070060 *
Eric Newberry17d7c472020-06-18 21:29:22 -070061 */
62 void
Eric Newberry1caa6342020-08-23 19:29:08 -070063 makeSignedInterest(Interest& interest,
64 SigningInfo params = SigningInfo(),
65 uint32_t signingFlags = WantNonce | WantTime);
Eric Newberry17d7c472020-06-18 21:29:22 -070066
67 /**
68 * @brief Creates and signs a command Interest
69 * @deprecated Use the new signed Interest format instead of command Interests. These can be
70 * created with makeSignedInterest.
71 *
72 * This generates a nonce and timestamp for the command Interest.
73 */
74 Interest
75 makeCommandInterest(Name name, const SigningInfo& params = SigningInfo());
76
77private:
78 /**
79 * @brief Get current timestamp, but ensure it is unique by increasing by 1 ms if already used
80 */
81 time::system_clock::TimePoint
82 getFreshTimestamp();
83
84private:
85 KeyChain& m_keyChain;
86 time::system_clock::TimePoint m_lastUsedTimestamp;
Eric Newberry1caa6342020-08-23 19:29:08 -070087 uint64_t m_lastUsedSeqNum;
Eric Newberry17d7c472020-06-18 21:29:22 -070088};
89
90} // namespace security
91} // namespace ndn
92
Davide Pesavento09904412021-03-24 16:40:53 -040093#endif // NDN_CXX_SECURITY_INTEREST_SIGNER_HPP