blob: 1af7885bf44bc7096bdc623840e1e567804b61c2 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shia9181d72017-08-13 16:47:45 +00002/*
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080020 */
21
22#ifndef NDN_SIGNATURE_HPP
23#define NDN_SIGNATURE_HPP
24
Yingdi Yu4a557052014-07-09 16:40:37 -070025#include "signature-info.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070026
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080027namespace ndn {
28
Junxiao Shia9181d72017-08-13 16:47:45 +000029/** @brief Holds SignatureInfo and SignatureValue in a Data packet
30 *
31 * A Signature is not a TLV element itself. It collects SignatureInfo and SignatureValue TLV
32 * elements together for easy access.
33 * In most cases, an application should use a subclass of Signature such as @p DigestSha256 , @p
34 * SignatureSha256WithRsa , or @p SignatureSha256WithEcdsa instead of using @p Signature type
35 * directly.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080036 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070037class Signature
38{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080039public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060040 class Error : public tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070041 {
42 public:
43 explicit
44 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060045 : tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070046 {
47 }
48 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080049
Yingdi Yu1b0311c2015-06-10 14:58:47 -070050 Signature() = default;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070051
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070052 explicit
Yingdi Yu4a557052014-07-09 16:40:37 -070053 Signature(const Block& info, const Block& value = Block());
54
55 explicit
56 Signature(const SignatureInfo& info, const Block& value = Block());
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080057
Junxiao Shia9181d72017-08-13 16:47:45 +000058 /** @brief Determine whether SignatureInfo is valid
59 */
60 explicit
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080061 operator bool() const
62 {
Yingdi Yu4a557052014-07-09 16:40:37 -070063 return m_info.getSignatureType() != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064 }
65
Junxiao Shia9181d72017-08-13 16:47:45 +000066 /** @brief Get SignatureInfo
Zhiyi Zhang387e57b2016-08-01 18:50:00 -070067 */
68 const SignatureInfo&
69 getSignatureInfo() const
70 {
71 return m_info;
72 }
73
Junxiao Shia9181d72017-08-13 16:47:45 +000074 /** @brief Get SignatureInfo as wire format
75 */
76 const Block&
77 getInfo() const
78 {
79 return m_info.wireEncode();
80 }
81
82 /** @brief Decode SignatureInfo from wire format
83 * @throw tlv::Error decode error
Yingdi Yu4a557052014-07-09 16:40:37 -070084 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080085 void
Yingdi Yu4a557052014-07-09 16:40:37 -070086 setInfo(const Block& info);
87
Junxiao Shia9181d72017-08-13 16:47:45 +000088 /** @brief Set SignatureInfo
Yingdi Yu1b0311c2015-06-10 14:58:47 -070089 */
Yingdi Yu4a557052014-07-09 16:40:37 -070090 void
91 setInfo(const SignatureInfo& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080092 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070093 m_info = info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070094 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095
Junxiao Shia9181d72017-08-13 16:47:45 +000096 /** @brief Get SignatureValue
Yingdi Yu1b0311c2015-06-10 14:58:47 -070097 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080098 const Block&
99 getValue() const
100 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700101 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800102 }
103
Junxiao Shia9181d72017-08-13 16:47:45 +0000104 /** @brief Set SignatureValue
105 * @throws tlv::Error TLV-TYPE of supplied block is not SignatureValue, or the block does not have TLV-VALUE
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700106 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107 void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700108 setValue(const Block& value);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800109
Junxiao Shia9181d72017-08-13 16:47:45 +0000110public: // SignatureInfo fields
111 /** @brief Get SignatureType
Junxiao Shi605671d2017-08-26 13:41:06 +0000112 * @throw Error signature is invalid
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700113 */
Junxiao Shi605671d2017-08-26 13:41:06 +0000114 tlv::SignatureTypeValue
115 getType() const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700116
Junxiao Shia9181d72017-08-13 16:47:45 +0000117 /** @brief Check if KeyLocator exists in SignatureInfo
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700118 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700119 bool
120 hasKeyLocator() const
121 {
122 return m_info.hasKeyLocator();
123 }
124
Junxiao Shia9181d72017-08-13 16:47:45 +0000125 /** @brief Get KeyLocator
126 * @throw tlv::Error KeyLocator does not exist in SignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700127 */
128 const KeyLocator&
129 getKeyLocator() const
130 {
131 return m_info.getKeyLocator();
132 }
133
Junxiao Shia9181d72017-08-13 16:47:45 +0000134 /** @brief Set KeyLocator
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700135 */
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700136 void
137 setKeyLocator(const KeyLocator& keyLocator)
138 {
139 m_info.setKeyLocator(keyLocator);
140 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700141
Junxiao Shia9181d72017-08-13 16:47:45 +0000142 /** @brief Unset KeyLocator
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700143 *
Junxiao Shia9181d72017-08-13 16:47:45 +0000144 * @note Subclasses of Signature may provide advisory (non-virtual) override to prevent unsetting
145 * KeyLocator if it is required by the specification.
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700146 */
147 void
148 unsetKeyLocator()
149 {
150 m_info.unsetKeyLocator();
151 }
152
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800153protected:
Yingdi Yu4a557052014-07-09 16:40:37 -0700154 SignatureInfo m_info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700155 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800156};
157
Junxiao Shi605671d2017-08-26 13:41:06 +0000158bool
159operator==(const Signature& lhs, const Signature& rhs);
Junxiao Shia9181d72017-08-13 16:47:45 +0000160
161inline bool
162operator!=(const Signature& lhs, const Signature& rhs)
163{
164 return !(lhs == rhs);
165}
166
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800167} // namespace ndn
168
169#endif // NDN_SIGNATURE_HPP