blob: 7cfc248822b79101953670ac05c062c4701cdcb5 [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shia9181d72017-08-13 16:47:45 +00002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu4a557052014-07-09 16:40:37 -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
22#ifndef NDN_SIGNATURE_INFO_HPP
23#define NDN_SIGNATURE_INFO_HPP
24
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/key-locator.hpp"
26#include "ndn-cxx/security/validity-period.hpp"
27
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -070028#include <list>
Yingdi Yu4a557052014-07-09 16:40:37 -070029
30namespace ndn {
31
Junxiao Shia9181d72017-08-13 16:47:45 +000032/** @brief Represents a SignatureInfo TLV element
33 */
Yingdi Yu4a557052014-07-09 16:40:37 -070034class SignatureInfo
35{
36public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060037 class Error : public tlv::Error
Yingdi Yu4a557052014-07-09 16:40:37 -070038 {
39 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060040 using tlv::Error::Error;
Yingdi Yu4a557052014-07-09 16:40:37 -070041 };
42
Junxiao Shia9181d72017-08-13 16:47:45 +000043 /** @brief Create an invalid SignatureInfo
44 */
Yingdi Yu4a557052014-07-09 16:40:37 -070045 SignatureInfo();
46
Junxiao Shia9181d72017-08-13 16:47:45 +000047 /** @brief Create with specified type
48 */
Yingdi Yu4a557052014-07-09 16:40:37 -070049 explicit
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060050 SignatureInfo(tlv::SignatureTypeValue type);
Yingdi Yu4a557052014-07-09 16:40:37 -070051
Junxiao Shia9181d72017-08-13 16:47:45 +000052 /** @brief Create with specified type and KeyLocator
53 */
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060054 SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator);
Yingdi Yu4a557052014-07-09 16:40:37 -070055
Junxiao Shia9181d72017-08-13 16:47:45 +000056 /** @brief Create from wire encoding
57 * @throw tlv::Error decode error
Yingdi Yu4a557052014-07-09 16:40:37 -070058 */
59 explicit
Junxiao Shia9181d72017-08-13 16:47:45 +000060 SignatureInfo(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070061
Junxiao Shia9181d72017-08-13 16:47:45 +000062 /** @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 Yu4a557052014-07-09 16:40:37 -070077 void
Junxiao Shia9181d72017-08-13 16:47:45 +000078 wireDecode(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070079
Junxiao Shia9181d72017-08-13 16:47:45 +000080public: // field access
81 /** @brief Get SignatureType
82 * @return tlv::SignatureTypeValue, or -1 to indicate invalid SignatureInfo
83 */
Yingdi Yu4a557052014-07-09 16:40:37 -070084 int32_t
85 getSignatureType() const
86 {
87 return m_type;
88 }
89
Junxiao Shia9181d72017-08-13 16:47:45 +000090 /** @brief Set SignatureType
91 */
92 void
93 setSignatureType(tlv::SignatureTypeValue type);
94
95 /** @brief Check if KeyLocator exists
96 */
Yingdi Yu4a557052014-07-09 16:40:37 -070097 bool
98 hasKeyLocator() const
99 {
100 return m_hasKeyLocator;
101 }
102
Junxiao Shia9181d72017-08-13 16:47:45 +0000103 /** @brief Get KeyLocator
104 * @throw Error KeyLocator does not exist
Yingdi Yu4a557052014-07-09 16:40:37 -0700105 */
106 const KeyLocator&
107 getKeyLocator() const;
108
Junxiao Shia9181d72017-08-13 16:47:45 +0000109 /** @brief Set KeyLocator
110 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700111 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000112 setKeyLocator(const KeyLocator& keyLocator);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700113
Junxiao Shia9181d72017-08-13 16:47:45 +0000114 /** @brief Unset KeyLocator
115 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700116 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000117 unsetKeyLocator();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700118
Junxiao Shia9181d72017-08-13 16:47:45 +0000119 /** @brief Get ValidityPeriod
120 * @throw Error ValidityPeriod does not exist
121 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700122 security::ValidityPeriod
123 getValidityPeriod() const;
124
Junxiao Shia9181d72017-08-13 16:47:45 +0000125 /** @brief Set ValidityPeriod
126 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700127 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000128 setValidityPeriod(const security::ValidityPeriod& validityPeriod);
Yingdi Yu4a557052014-07-09 16:40:37 -0700129
Junxiao Shia9181d72017-08-13 16:47:45 +0000130 /** @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 Yu4a557052014-07-09 16:40:37 -0700138 */
139 const Block&
140 getTypeSpecificTlv(uint32_t type) const;
141
Junxiao Shia9181d72017-08-13 16:47:45 +0000142 /** @brief Append SignatureType-specific sub-element
143 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700144 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000145 appendTypeSpecificTlv(const Block& element);
Yingdi Yu4a557052014-07-09 16:40:37 -0700146
147private:
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 Afanasyev41469752017-01-10 21:51:55 -0800154
Junxiao Shia9181d72017-08-13 16:47:45 +0000155 friend bool
156 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
157
Alexander Afanasyev41469752017-01-10 21:51:55 -0800158 friend std::ostream&
159 operator<<(std::ostream& os, const SignatureInfo& info);
Yingdi Yu4a557052014-07-09 16:40:37 -0700160};
161
Davide Pesavento88a0d812017-08-19 21:31:42 -0400162NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(SignatureInfo);
163
Junxiao Shia9181d72017-08-13 16:47:45 +0000164bool
165operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
166
167inline bool
168operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
169{
170 return !(lhs == rhs);
171}
172
Alexander Afanasyev41469752017-01-10 21:51:55 -0800173std::ostream&
174operator<<(std::ostream& os, const SignatureInfo& info);
175
Yingdi Yu4a557052014-07-09 16:40:37 -0700176} // namespace ndn
177
178#endif // NDN_SIGNATURE_INFO_HPP