Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -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 | |
| 22 | #ifndef NDN_SIGNATURE_INFO_HPP |
| 23 | #define NDN_SIGNATURE_INFO_HPP |
| 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame^] | 25 | #include "ndn-cxx/key-locator.hpp" |
| 26 | #include "ndn-cxx/security/validity-period.hpp" |
| 27 | |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 28 | #include <list> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 32 | /** @brief Represents a SignatureInfo TLV element |
| 33 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 34 | class SignatureInfo |
| 35 | { |
| 36 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 37 | class Error : public tlv::Error |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 40 | using tlv::Error::Error; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 43 | /** @brief Create an invalid SignatureInfo |
| 44 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 45 | SignatureInfo(); |
| 46 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 47 | /** @brief Create with specified type |
| 48 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 49 | explicit |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 50 | SignatureInfo(tlv::SignatureTypeValue type); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 52 | /** @brief Create with specified type and KeyLocator |
| 53 | */ |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 54 | SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 56 | /** @brief Create from wire encoding |
| 57 | * @throw tlv::Error decode error |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 58 | */ |
| 59 | explicit |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 60 | SignatureInfo(const Block& wire); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 62 | /** @brief Fast encoding or block size estimation |
| 63 | * @param encoder EncodingEstimator or EncodingBuffer instance |
| 64 | */ |
| 65 | template<encoding::Tag TAG> |
| 66 | size_t |
| 67 | wireEncode(EncodingImpl<TAG>& encoder) const; |
| 68 | |
| 69 | /** @brief Encode to wire format |
| 70 | */ |
| 71 | const Block& |
| 72 | wireEncode() const; |
| 73 | |
| 74 | /** @brief Decode from wire format |
| 75 | * @throw tlv::Error decode error |
| 76 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 77 | void |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 78 | wireDecode(const Block& wire); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 79 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 80 | public: // field access |
| 81 | /** @brief Get SignatureType |
| 82 | * @return tlv::SignatureTypeValue, or -1 to indicate invalid SignatureInfo |
| 83 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 84 | int32_t |
| 85 | getSignatureType() const |
| 86 | { |
| 87 | return m_type; |
| 88 | } |
| 89 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 90 | /** @brief Set SignatureType |
| 91 | */ |
| 92 | void |
| 93 | setSignatureType(tlv::SignatureTypeValue type); |
| 94 | |
| 95 | /** @brief Check if KeyLocator exists |
| 96 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 97 | bool |
| 98 | hasKeyLocator() const |
| 99 | { |
| 100 | return m_hasKeyLocator; |
| 101 | } |
| 102 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 103 | /** @brief Get KeyLocator |
| 104 | * @throw Error KeyLocator does not exist |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 105 | */ |
| 106 | const KeyLocator& |
| 107 | getKeyLocator() const; |
| 108 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 109 | /** @brief Set KeyLocator |
| 110 | */ |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 111 | void |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 112 | setKeyLocator(const KeyLocator& keyLocator); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 114 | /** @brief Unset KeyLocator |
| 115 | */ |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 116 | void |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 117 | unsetKeyLocator(); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 119 | /** @brief Get ValidityPeriod |
| 120 | * @throw Error ValidityPeriod does not exist |
| 121 | */ |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 122 | security::ValidityPeriod |
| 123 | getValidityPeriod() const; |
| 124 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 125 | /** @brief Set ValidityPeriod |
| 126 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 127 | void |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 128 | setValidityPeriod(const security::ValidityPeriod& validityPeriod); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 129 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 130 | /** @brief Unset ValidityPeriod |
| 131 | */ |
| 132 | void |
| 133 | unsetValidityPeriod(); |
| 134 | |
| 135 | /** @brief Get SignatureType-specific sub-element |
| 136 | * @param type TLV-TYPE of sub-element |
| 137 | * @throw Error sub-element of specified type does not exist |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 138 | */ |
| 139 | const Block& |
| 140 | getTypeSpecificTlv(uint32_t type) const; |
| 141 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 142 | /** @brief Append SignatureType-specific sub-element |
| 143 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 144 | void |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 145 | appendTypeSpecificTlv(const Block& element); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | int32_t m_type; |
| 149 | bool m_hasKeyLocator; |
| 150 | KeyLocator m_keyLocator; |
| 151 | std::list<Block> m_otherTlvs; |
| 152 | |
| 153 | mutable Block m_wire; |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 154 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 155 | friend bool |
| 156 | operator==(const SignatureInfo& lhs, const SignatureInfo& rhs); |
| 157 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 158 | friend std::ostream& |
| 159 | operator<<(std::ostream& os, const SignatureInfo& info); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 162 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(SignatureInfo); |
| 163 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 164 | bool |
| 165 | operator==(const SignatureInfo& lhs, const SignatureInfo& rhs); |
| 166 | |
| 167 | inline bool |
| 168 | operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs) |
| 169 | { |
| 170 | return !(lhs == rhs); |
| 171 | } |
| 172 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 173 | std::ostream& |
| 174 | operator<<(std::ostream& os, const SignatureInfo& info); |
| 175 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 176 | } // namespace ndn |
| 177 | |
| 178 | #endif // NDN_SIGNATURE_INFO_HPP |