Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 22 | #ifndef NDN_CXX_SECURITY_INTEREST_SIGNER_HPP |
| 23 | #define NDN_CXX_SECURITY_INTEREST_SIGNER_HPP |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ndn-cxx/security/key-chain.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | |
| 30 | /** |
| 31 | * @brief Helper class to create signed Interests |
| 32 | * |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 33 | * The signer generates signature elements for an Interest and signs it with the KeyChain. |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 34 | */ |
| 35 | class InterestSigner |
| 36 | { |
| 37 | public: |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 38 | /** |
| 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 | |
| 49 | public: |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 50 | explicit |
| 51 | InterestSigner(KeyChain& keyChain); |
| 52 | |
| 53 | /** |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 54 | * @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 Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 60 | * |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 61 | */ |
| 62 | void |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 63 | makeSignedInterest(Interest& interest, |
| 64 | SigningInfo params = SigningInfo(), |
| 65 | uint32_t signingFlags = WantNonce | WantTime); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 66 | |
| 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 | |
| 77 | private: |
| 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 | |
| 84 | private: |
| 85 | KeyChain& m_keyChain; |
| 86 | time::system_clock::TimePoint m_lastUsedTimestamp; |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 87 | uint64_t m_lastUsedSeqNum; |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace security |
| 91 | } // namespace ndn |
| 92 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 93 | #endif // NDN_CXX_SECURITY_INTEREST_SIGNER_HPP |