blob: 5970270117809118ab9182d69f5297e10ce29dcf [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -070025#include "common.hpp"
Yingdi Yu4a557052014-07-09 16:40:37 -070026#include "signature-info.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070027
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080028namespace ndn {
29
30/**
31 * A Signature is storage for the signature-related information (info and value) in a Data packet.
32 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070033class Signature
34{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080035public:
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060036 class Error : public tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070037 {
38 public:
39 explicit
40 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060041 : tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070042 {
43 }
44 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080045
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060046 /// @deprecated use tlv::SignatureTypeValue instead.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080047 enum {
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060048 Sha256 = tlv::DigestSha256,
49 Sha256WithRsa = tlv::SignatureSha256WithRsa,
50 Sha256WithEcdsa = tlv::SignatureSha256WithEcdsa
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080051 };
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070052
Yingdi Yu1b0311c2015-06-10 14:58:47 -070053 Signature() = default;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070054
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070055 explicit
Yingdi Yu4a557052014-07-09 16:40:37 -070056 Signature(const Block& info, const Block& value = Block());
57
58 explicit
59 Signature(const SignatureInfo& info, const Block& value = Block());
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060
61 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
Yingdi Yu1b0311c2015-06-10 14:58:47 -070066 /**
67 * @brief Get SignatureInfo in the wire format
68 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069 const Block&
70 getInfo() const
71 {
Yingdi Yu4a557052014-07-09 16:40:37 -070072 return m_info.wireEncode(); // will do nothing if wire already exists
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080073 }
74
Yingdi Yu4a557052014-07-09 16:40:37 -070075 /**
76 * @brief Set SignatureInfo from a block
77 *
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060078 * @throws tlv::Error if supplied block is not formatted correctly
Yingdi Yu4a557052014-07-09 16:40:37 -070079 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080 void
Yingdi Yu4a557052014-07-09 16:40:37 -070081 setInfo(const Block& info);
82
Yingdi Yu1b0311c2015-06-10 14:58:47 -070083 /**
84 * @brief Set SignatureInfo
85 */
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
Yingdi Yu1b0311c2015-06-10 14:58:47 -070092 /**
93 * @brief Get SignatureValue in the wire format
94 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095 const Block&
96 getValue() const
97 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070098 m_value.encode(); // will do nothing if wire already exists
99 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800100 }
101
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700102 /**
103 * @brief Get SignatureValue from a block
104 *
105 * @throws tlv::Error if supplied block has type different from SignatureValue
106 */
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
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700110 /**
111 * @brief Get signature type
112 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700113 uint32_t
114 getType() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800115 {
Yingdi Yu4a557052014-07-09 16:40:37 -0700116 return m_info.getSignatureType();
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700117 }
118
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700119 /**
120 * @brief Check if SignatureInfo block has a KeyLocator
121 */
Yingdi Yu4a557052014-07-09 16:40:37 -0700122 bool
123 hasKeyLocator() const
124 {
125 return m_info.hasKeyLocator();
126 }
127
128 /**
129 * @brief Get KeyLocator
130 *
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700131 * @throws Signature::Error if KeyLocator does not exist
Yingdi Yu4a557052014-07-09 16:40:37 -0700132 */
133 const KeyLocator&
134 getKeyLocator() const
135 {
136 return m_info.getKeyLocator();
137 }
138
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700139 /**
140 * @brief Set KeyLocator
141 */
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700142 void
143 setKeyLocator(const KeyLocator& keyLocator)
144 {
145 m_info.setKeyLocator(keyLocator);
146 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700147
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700148 /**
149 * @brief Unset KeyLocator
150 *
151 * Note that specific signature types may provide advisory (non-virtual) override
152 * to prevent unsetting KeyLocator if it is required by the specification.
153 */
154 void
155 unsetKeyLocator()
156 {
157 m_info.unsetKeyLocator();
158 }
159
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700160public: // EqualityComparable concept
161 bool
162 operator==(const Signature& other) const
163 {
164 return getInfo() == other.getInfo() &&
165 getValue() == other.getValue();
166 }
167
168 bool
169 operator!=(const Signature& other) const
170 {
171 return !(*this == other);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800172 }
173
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800174protected:
Yingdi Yu4a557052014-07-09 16:40:37 -0700175 SignatureInfo m_info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700176 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800177};
178
179} // namespace ndn
180
181#endif // NDN_SIGNATURE_HPP