blob: 7ac8404a8c64fedef49797ea69c424758eeb2610 [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/*
Alexander Afanasyev41469752017-01-10 21:51:55 -08003 * Copyright (c) 2013-2017 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
Yingdi Yu4a557052014-07-09 16:40:37 -070025#include "key-locator.hpp"
Yingdi Yu6be43f32015-06-09 14:19:54 -070026#include "security/validity-period.hpp"
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -070027#include <list>
Yingdi Yu4a557052014-07-09 16:40:37 -070028
29namespace ndn {
30
Junxiao Shia9181d72017-08-13 16:47:45 +000031/** @brief Represents a SignatureInfo TLV element
32 */
Yingdi Yu4a557052014-07-09 16:40:37 -070033class SignatureInfo
34{
35public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060036 class Error : public tlv::Error
Yingdi Yu4a557052014-07-09 16:40:37 -070037 {
38 public:
39 explicit
40 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041 : tlv::Error(what)
Yingdi Yu4a557052014-07-09 16:40:37 -070042 {
43 }
44 };
45
Junxiao Shia9181d72017-08-13 16:47:45 +000046 /** @brief Create an invalid SignatureInfo
47 */
Yingdi Yu4a557052014-07-09 16:40:37 -070048 SignatureInfo();
49
Junxiao Shia9181d72017-08-13 16:47:45 +000050 /** @brief Create with specified type
51 */
Yingdi Yu4a557052014-07-09 16:40:37 -070052 explicit
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060053 SignatureInfo(tlv::SignatureTypeValue type);
Yingdi Yu4a557052014-07-09 16:40:37 -070054
Junxiao Shia9181d72017-08-13 16:47:45 +000055 /** @brief Create with specified type and KeyLocator
56 */
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060057 SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator);
Yingdi Yu4a557052014-07-09 16:40:37 -070058
Junxiao Shia9181d72017-08-13 16:47:45 +000059 /** @brief Create from wire encoding
60 * @throw tlv::Error decode error
Yingdi Yu4a557052014-07-09 16:40:37 -070061 */
62 explicit
Junxiao Shia9181d72017-08-13 16:47:45 +000063 SignatureInfo(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070064
Junxiao Shia9181d72017-08-13 16:47:45 +000065 /** @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 Yu4a557052014-07-09 16:40:37 -070080 void
Junxiao Shia9181d72017-08-13 16:47:45 +000081 wireDecode(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070082
Junxiao Shia9181d72017-08-13 16:47:45 +000083public: // field access
84 /** @brief Get SignatureType
85 * @return tlv::SignatureTypeValue, or -1 to indicate invalid SignatureInfo
86 */
Yingdi Yu4a557052014-07-09 16:40:37 -070087 int32_t
88 getSignatureType() const
89 {
90 return m_type;
91 }
92
Junxiao Shia9181d72017-08-13 16:47:45 +000093 /** @brief Set SignatureType
94 */
95 void
96 setSignatureType(tlv::SignatureTypeValue type);
97
98 /** @brief Check if KeyLocator exists
99 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700100 bool
101 hasKeyLocator() const
102 {
103 return m_hasKeyLocator;
104 }
105
Junxiao Shia9181d72017-08-13 16:47:45 +0000106 /** @brief Get KeyLocator
107 * @throw Error KeyLocator does not exist
Yingdi Yu4a557052014-07-09 16:40:37 -0700108 */
109 const KeyLocator&
110 getKeyLocator() const;
111
Junxiao Shia9181d72017-08-13 16:47:45 +0000112 /** @brief Set KeyLocator
113 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700114 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000115 setKeyLocator(const KeyLocator& keyLocator);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700116
Junxiao Shia9181d72017-08-13 16:47:45 +0000117 /** @brief Unset KeyLocator
118 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700119 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000120 unsetKeyLocator();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700121
Junxiao Shia9181d72017-08-13 16:47:45 +0000122 /** @brief Get ValidityPeriod
123 * @throw Error ValidityPeriod does not exist
124 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700125 security::ValidityPeriod
126 getValidityPeriod() const;
127
Junxiao Shia9181d72017-08-13 16:47:45 +0000128 /** @brief Set ValidityPeriod
129 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700130 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000131 setValidityPeriod(const security::ValidityPeriod& validityPeriod);
Yingdi Yu4a557052014-07-09 16:40:37 -0700132
Junxiao Shia9181d72017-08-13 16:47:45 +0000133 /** @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 Yu4a557052014-07-09 16:40:37 -0700141 */
142 const Block&
143 getTypeSpecificTlv(uint32_t type) const;
144
Junxiao Shia9181d72017-08-13 16:47:45 +0000145 /** @brief Append SignatureType-specific sub-element
146 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700147 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000148 appendTypeSpecificTlv(const Block& element);
Yingdi Yu4a557052014-07-09 16:40:37 -0700149
150private:
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 Afanasyev41469752017-01-10 21:51:55 -0800157
Junxiao Shia9181d72017-08-13 16:47:45 +0000158 friend bool
159 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
160
Alexander Afanasyev41469752017-01-10 21:51:55 -0800161 friend std::ostream&
162 operator<<(std::ostream& os, const SignatureInfo& info);
Yingdi Yu4a557052014-07-09 16:40:37 -0700163};
164
Junxiao Shia9181d72017-08-13 16:47:45 +0000165bool
166operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
167
168inline bool
169operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
170{
171 return !(lhs == rhs);
172}
173
Alexander Afanasyev41469752017-01-10 21:51:55 -0800174std::ostream&
175operator<<(std::ostream& os, const SignatureInfo& info);
176
Yingdi Yu4a557052014-07-09 16:40:37 -0700177} // namespace ndn
178
179#endif // NDN_SIGNATURE_INFO_HPP