blob: f7853dd6d7068a8877de336dd2cfaa92ae791c36 [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
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070025#include "encoding/tlv.hpp"
26
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080027namespace ndn {
28
29/**
30 * A Signature is storage for the signature-related information (info and value) in a Data packet.
31 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070032class Signature
33{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080034public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070035 class Error : public Tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070036 {
37 public:
38 explicit
39 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070040 : Tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070041 {
42 }
43 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080044
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070045 /// @deprecated use Tlv::SignatureTypeValue instead.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046 enum {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070047 Sha256 = Tlv::DigestSha256,
48 Sha256WithRsa = Tlv::SignatureSha256WithRsa,
49 Sha256WithEcdsa = Tlv::SignatureSha256WithEcdsa
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080050 };
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070051
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080052 Signature()
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070053 : m_type(-1)
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
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070058 Signature(const Block& info, const Block& value = Block())
59 : m_value(value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060 {
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080061 setInfo(info);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080062 }
63
64 operator bool() const
65 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070066 return m_type != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080067 }
68
69 uint32_t
70 getType() const
71 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070072 return m_type;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080073 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070074
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080075 const Block&
76 getInfo() const
77 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070078 m_info.encode(); // will do nothing if wire already exists
79 return m_info;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080 }
81
82 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070083 setInfo(const Block& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080084 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070085 m_info = info;
86 if (m_info.hasWire() || m_info.hasValue())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080087 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070088 m_info.parse();
89 const Block& signatureType = m_info.get(Tlv::SignatureType);
90 m_type = readNonNegativeInteger(signatureType);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080091 }
92 else
93 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070094 m_type = -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070096 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080097
98 const Block&
99 getValue() const
100 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700101 m_value.encode(); // will do nothing if wire already exists
102 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800103 }
104
105 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700106 setValue(const Block& value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700108 m_value = value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800109 }
110
111 void
112 reset()
113 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 m_type = -1;
115 m_info.reset();
116 m_value.reset();
117 }
118
119public: // EqualityComparable concept
120 bool
121 operator==(const Signature& other) const
122 {
123 return getInfo() == other.getInfo() &&
124 getValue() == other.getValue();
125 }
126
127 bool
128 operator!=(const Signature& other) const
129 {
130 return !(*this == other);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800131 }
132
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800133protected:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700134 int32_t m_type;
135
136 mutable Block m_info;
137 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800138};
139
140} // namespace ndn
141
142#endif // NDN_SIGNATURE_HPP