blob: 1a6eeb71878de13ab68c472c2f8fb8f0b6fc52c3 [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
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:
Junxiao Shi68b53852018-07-25 13:56:38 -060039 using tlv::Error::Error;
Yingdi Yu4a557052014-07-09 16:40:37 -070040 };
41
Junxiao Shia9181d72017-08-13 16:47:45 +000042 /** @brief Create an invalid SignatureInfo
43 */
Yingdi Yu4a557052014-07-09 16:40:37 -070044 SignatureInfo();
45
Junxiao Shia9181d72017-08-13 16:47:45 +000046 /** @brief Create with specified type
47 */
Yingdi Yu4a557052014-07-09 16:40:37 -070048 explicit
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060049 SignatureInfo(tlv::SignatureTypeValue type);
Yingdi Yu4a557052014-07-09 16:40:37 -070050
Junxiao Shia9181d72017-08-13 16:47:45 +000051 /** @brief Create with specified type and KeyLocator
52 */
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060053 SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator);
Yingdi Yu4a557052014-07-09 16:40:37 -070054
Junxiao Shia9181d72017-08-13 16:47:45 +000055 /** @brief Create from wire encoding
56 * @throw tlv::Error decode error
Yingdi Yu4a557052014-07-09 16:40:37 -070057 */
58 explicit
Junxiao Shia9181d72017-08-13 16:47:45 +000059 SignatureInfo(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070060
Junxiao Shia9181d72017-08-13 16:47:45 +000061 /** @brief Fast encoding or block size estimation
62 * @param encoder EncodingEstimator or EncodingBuffer instance
63 */
64 template<encoding::Tag TAG>
65 size_t
66 wireEncode(EncodingImpl<TAG>& encoder) const;
67
68 /** @brief Encode to wire format
69 */
70 const Block&
71 wireEncode() const;
72
73 /** @brief Decode from wire format
74 * @throw tlv::Error decode error
75 */
Yingdi Yu4a557052014-07-09 16:40:37 -070076 void
Junxiao Shia9181d72017-08-13 16:47:45 +000077 wireDecode(const Block& wire);
Yingdi Yu4a557052014-07-09 16:40:37 -070078
Junxiao Shia9181d72017-08-13 16:47:45 +000079public: // field access
80 /** @brief Get SignatureType
81 * @return tlv::SignatureTypeValue, or -1 to indicate invalid SignatureInfo
82 */
Yingdi Yu4a557052014-07-09 16:40:37 -070083 int32_t
84 getSignatureType() const
85 {
86 return m_type;
87 }
88
Junxiao Shia9181d72017-08-13 16:47:45 +000089 /** @brief Set SignatureType
90 */
91 void
92 setSignatureType(tlv::SignatureTypeValue type);
93
94 /** @brief Check if KeyLocator exists
95 */
Yingdi Yu4a557052014-07-09 16:40:37 -070096 bool
97 hasKeyLocator() const
98 {
99 return m_hasKeyLocator;
100 }
101
Junxiao Shia9181d72017-08-13 16:47:45 +0000102 /** @brief Get KeyLocator
103 * @throw Error KeyLocator does not exist
Yingdi Yu4a557052014-07-09 16:40:37 -0700104 */
105 const KeyLocator&
106 getKeyLocator() const;
107
Junxiao Shia9181d72017-08-13 16:47:45 +0000108 /** @brief Set KeyLocator
109 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700110 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000111 setKeyLocator(const KeyLocator& keyLocator);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700112
Junxiao Shia9181d72017-08-13 16:47:45 +0000113 /** @brief Unset KeyLocator
114 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700115 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000116 unsetKeyLocator();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700117
Junxiao Shia9181d72017-08-13 16:47:45 +0000118 /** @brief Get ValidityPeriod
119 * @throw Error ValidityPeriod does not exist
120 */
Yingdi Yu6be43f32015-06-09 14:19:54 -0700121 security::ValidityPeriod
122 getValidityPeriod() const;
123
Junxiao Shia9181d72017-08-13 16:47:45 +0000124 /** @brief Set ValidityPeriod
125 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700126 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000127 setValidityPeriod(const security::ValidityPeriod& validityPeriod);
Yingdi Yu4a557052014-07-09 16:40:37 -0700128
Junxiao Shia9181d72017-08-13 16:47:45 +0000129 /** @brief Unset ValidityPeriod
130 */
131 void
132 unsetValidityPeriod();
133
134 /** @brief Get SignatureType-specific sub-element
135 * @param type TLV-TYPE of sub-element
136 * @throw Error sub-element of specified type does not exist
Yingdi Yu4a557052014-07-09 16:40:37 -0700137 */
138 const Block&
139 getTypeSpecificTlv(uint32_t type) const;
140
Junxiao Shia9181d72017-08-13 16:47:45 +0000141 /** @brief Append SignatureType-specific sub-element
142 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700143 void
Junxiao Shia9181d72017-08-13 16:47:45 +0000144 appendTypeSpecificTlv(const Block& element);
Yingdi Yu4a557052014-07-09 16:40:37 -0700145
146private:
147 int32_t m_type;
148 bool m_hasKeyLocator;
149 KeyLocator m_keyLocator;
150 std::list<Block> m_otherTlvs;
151
152 mutable Block m_wire;
Alexander Afanasyev41469752017-01-10 21:51:55 -0800153
Junxiao Shia9181d72017-08-13 16:47:45 +0000154 friend bool
155 operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
156
Alexander Afanasyev41469752017-01-10 21:51:55 -0800157 friend std::ostream&
158 operator<<(std::ostream& os, const SignatureInfo& info);
Yingdi Yu4a557052014-07-09 16:40:37 -0700159};
160
Davide Pesavento88a0d812017-08-19 21:31:42 -0400161NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(SignatureInfo);
162
Junxiao Shia9181d72017-08-13 16:47:45 +0000163bool
164operator==(const SignatureInfo& lhs, const SignatureInfo& rhs);
165
166inline bool
167operator!=(const SignatureInfo& lhs, const SignatureInfo& rhs)
168{
169 return !(lhs == rhs);
170}
171
Alexander Afanasyev41469752017-01-10 21:51:55 -0800172std::ostream&
173operator<<(std::ostream& os, const SignatureInfo& info);
174
Yingdi Yu4a557052014-07-09 16:40:37 -0700175} // namespace ndn
176
177#endif // NDN_SIGNATURE_INFO_HPP