blob: 3d3c4c961ec7d9fc9551c953a49a3e7bf1f605b0 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -07003 * Copyright (C) 2013-2014 Regents of the University of California.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_SIGNATURE_HPP
8#define NDN_SIGNATURE_HPP
9
10namespace ndn {
11
12/**
13 * A Signature is storage for the signature-related information (info and value) in a Data packet.
14 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070015class Signature
16{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080017public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070018 class Error : public Tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070019 {
20 public:
21 explicit
22 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070023 : Tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070024 {
25 }
26 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080027
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080028 enum {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080029 Sha256 = 0,
30 Sha256WithRsa = 1
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080031 };
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070032
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080033 Signature()
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070034 : m_type(-1)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080035 {
36 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070037
38 Signature(const Block& info, const Block& value = Block())
39 : m_value(value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080040 {
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080041 setInfo(info);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080042 }
43
44 operator bool() const
45 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070046 return m_type != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080047 }
48
49 uint32_t
50 getType() const
51 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070052 return m_type;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070054
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055 const Block&
56 getInfo() const
57 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070058 m_info.encode(); // will do nothing if wire already exists
59 return m_info;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060 }
61
62 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070063 setInfo(const Block& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070065 m_info = info;
66 if (m_info.hasWire() || m_info.hasValue())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080067 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070068 m_info.parse();
69 const Block& signatureType = m_info.get(Tlv::SignatureType);
70 m_type = readNonNegativeInteger(signatureType);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080071 }
72 else
73 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070074 m_type = -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080075 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070076 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080077
78 const Block&
79 getValue() const
80 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070081 m_value.encode(); // will do nothing if wire already exists
82 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080083 }
84
85 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070086 setValue(const Block& value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080087 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070088 m_value = value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080089 }
90
91 void
92 reset()
93 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070094 m_type = -1;
95 m_info.reset();
96 m_value.reset();
97 }
98
99public: // EqualityComparable concept
100 bool
101 operator==(const Signature& other) const
102 {
103 return getInfo() == other.getInfo() &&
104 getValue() == other.getValue();
105 }
106
107 bool
108 operator!=(const Signature& other) const
109 {
110 return !(*this == other);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800111 }
112
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800113protected:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 int32_t m_type;
115
116 mutable Block m_info;
117 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800118};
119
120} // namespace ndn
121
122#endif // NDN_SIGNATURE_HPP