blob: 1c051c808cb98e954b4949f80c9057c6680c0d55 [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/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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:
Junxiao Shi68b53852018-07-25 13:56:38 -060043 using tlv::Error::Error;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070044 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080045
Yingdi Yu1b0311c2015-06-10 14:58:47 -070046 Signature() = default;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070047
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070048 explicit
Yingdi Yu4a557052014-07-09 16:40:37 -070049 Signature(const Block& info, const Block& value = Block());
50
51 explicit
52 Signature(const SignatureInfo& info, const Block& value = Block());
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053
Junxiao Shia9181d72017-08-13 16:47:45 +000054 /** @brief Determine whether SignatureInfo is valid
55 */
56 explicit
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080057 operator bool() const
58 {
Yingdi Yu4a557052014-07-09 16:40:37 -070059 return m_info.getSignatureType() != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060 }
61
Junxiao Shia9181d72017-08-13 16:47:45 +000062 /** @brief Get SignatureInfo
Zhiyi Zhang387e57b2016-08-01 18:50:00 -070063 */
64 const SignatureInfo&
65 getSignatureInfo() const
66 {
67 return m_info;
68 }
69
Junxiao Shia9181d72017-08-13 16:47:45 +000070 /** @brief Get SignatureInfo as wire format
71 */
72 const Block&
73 getInfo() const
74 {
75 return m_info.wireEncode();
76 }
77
78 /** @brief Decode SignatureInfo from wire format
79 * @throw tlv::Error decode error
Yingdi Yu4a557052014-07-09 16:40:37 -070080 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080081 void
Yingdi Yu4a557052014-07-09 16:40:37 -070082 setInfo(const Block& info);
83
Junxiao Shia9181d72017-08-13 16:47:45 +000084 /** @brief Set SignatureInfo
Yingdi Yu1b0311c2015-06-10 14:58:47 -070085 */
Yingdi Yu4a557052014-07-09 16:40:37 -070086 void
87 setInfo(const SignatureInfo& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080088 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070089 m_info = info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070090 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080091
Junxiao Shia9181d72017-08-13 16:47:45 +000092 /** @brief Get SignatureValue
Yingdi Yu1b0311c2015-06-10 14:58:47 -070093 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080094 const Block&
95 getValue() const
96 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070097 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080098 }
99
Junxiao Shia9181d72017-08-13 16:47:45 +0000100 /** @brief Set SignatureValue
101 * @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 -0700102 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800103 void
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700104 setValue(const Block& value);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800105
Junxiao Shia9181d72017-08-13 16:47:45 +0000106public: // SignatureInfo fields
107 /** @brief Get SignatureType
Junxiao Shi605671d2017-08-26 13:41:06 +0000108 * @throw Error signature is invalid
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700109 */
Junxiao Shi605671d2017-08-26 13:41:06 +0000110 tlv::SignatureTypeValue
111 getType() const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700112
Junxiao Shia9181d72017-08-13 16:47:45 +0000113 /** @brief Check if KeyLocator exists in SignatureInfo
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700114 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700115 bool
116 hasKeyLocator() const
117 {
118 return m_info.hasKeyLocator();
119 }
120
Junxiao Shia9181d72017-08-13 16:47:45 +0000121 /** @brief Get KeyLocator
122 * @throw tlv::Error KeyLocator does not exist in SignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700123 */
124 const KeyLocator&
125 getKeyLocator() const
126 {
127 return m_info.getKeyLocator();
128 }
129
Junxiao Shia9181d72017-08-13 16:47:45 +0000130 /** @brief Set KeyLocator
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700131 */
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700132 void
133 setKeyLocator(const KeyLocator& keyLocator)
134 {
135 m_info.setKeyLocator(keyLocator);
136 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700137
Junxiao Shia9181d72017-08-13 16:47:45 +0000138 /** @brief Unset KeyLocator
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700139 *
Junxiao Shia9181d72017-08-13 16:47:45 +0000140 * @note Subclasses of Signature may provide advisory (non-virtual) override to prevent unsetting
141 * KeyLocator if it is required by the specification.
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700142 */
143 void
144 unsetKeyLocator()
145 {
146 m_info.unsetKeyLocator();
147 }
148
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800149protected:
Yingdi Yu4a557052014-07-09 16:40:37 -0700150 SignatureInfo m_info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700151 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800152};
153
Junxiao Shi605671d2017-08-26 13:41:06 +0000154bool
155operator==(const Signature& lhs, const Signature& rhs);
Junxiao Shia9181d72017-08-13 16:47:45 +0000156
157inline bool
158operator!=(const Signature& lhs, const Signature& rhs)
159{
160 return !(lhs == rhs);
161}
162
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800163} // namespace ndn
164
165#endif // NDN_SIGNATURE_HPP