blob: 66a3f5206950366aca906bca98c77001cd2514d3 [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:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070036 class Error : public Tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070037 {
38 public:
39 explicit
40 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070041 : Tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070042 {
43 }
44 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080045
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070046 /// @deprecated use Tlv::SignatureTypeValue instead.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080047 enum {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070048 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
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053 Signature()
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080054 {
55 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070056
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070057 explicit
Yingdi Yu4a557052014-07-09 16:40:37 -070058 Signature(const Block& info, const Block& value = Block());
59
60 explicit
61 Signature(const SignatureInfo& info, const Block& value = Block());
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080062
63 operator bool() const
64 {
Yingdi Yu4a557052014-07-09 16:40:37 -070065 return m_info.getSignatureType() != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080066 }
67
Yingdi Yu4a557052014-07-09 16:40:37 -070068 /// @brief Get SignatureInfo in the wire format
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 *
78 * @throws Tlv::Error if supplied block is not formatted correctly
79 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080 void
Yingdi Yu4a557052014-07-09 16:40:37 -070081 setInfo(const Block& info);
82
83 /// @brief Set SignatureInfo
84 void
85 setInfo(const SignatureInfo& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080086 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 m_info = info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070088 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080089
90 const Block&
91 getValue() const
92 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070093 m_value.encode(); // will do nothing if wire already exists
94 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095 }
96
97 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070098 setValue(const Block& value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080099 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700100 m_value = value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800101 }
102
Yingdi Yu4a557052014-07-09 16:40:37 -0700103 uint32_t
104 getType() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800105 {
Yingdi Yu4a557052014-07-09 16:40:37 -0700106 return m_info.getSignatureType();
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700107 }
108
Yingdi Yu4a557052014-07-09 16:40:37 -0700109 bool
110 hasKeyLocator() const
111 {
112 return m_info.hasKeyLocator();
113 }
114
115 /**
116 * @brief Get KeyLocator
117 *
118 * @throws Signature::Error if keyLocator does not exist
119 */
120 const KeyLocator&
121 getKeyLocator() const
122 {
123 return m_info.getKeyLocator();
124 }
125
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700126 /// @brief Set KeyLocator
127 void
128 setKeyLocator(const KeyLocator& keyLocator)
129 {
130 m_info.setKeyLocator(keyLocator);
131 }
Yingdi Yu4a557052014-07-09 16:40:37 -0700132
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700133public: // EqualityComparable concept
134 bool
135 operator==(const Signature& other) const
136 {
137 return getInfo() == other.getInfo() &&
138 getValue() == other.getValue();
139 }
140
141 bool
142 operator!=(const Signature& other) const
143 {
144 return !(*this == other);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800145 }
146
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800147protected:
Yingdi Yu4a557052014-07-09 16:40:37 -0700148 SignatureInfo m_info;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700149 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800150};
151
152} // namespace ndn
153
154#endif // NDN_SIGNATURE_HPP