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