Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
2 | /** | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
5 | */ | ||||
6 | |||||
7 | #ifndef NDN_SIGNATURE_HPP | ||||
8 | #define NDN_SIGNATURE_HPP | ||||
9 | |||||
10 | namespace ndn { | ||||
11 | |||||
12 | /** | ||||
13 | * A Signature is storage for the signature-related information (info and value) in a Data packet. | ||||
14 | */ | ||||
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 15 | class Signature |
16 | { | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 17 | public: |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 18 | class Error : public Tlv::Error |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 19 | { |
20 | public: | ||||
21 | explicit | ||||
22 | Error(const std::string& what) | ||||
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 23 | : Tlv::Error(what) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 24 | { |
25 | } | ||||
26 | }; | ||||
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 28 | enum { |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 29 | Sha256 = 0, |
30 | Sha256WithRsa = 1 | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 31 | }; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 33 | Signature() |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 34 | : m_type(-1) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 35 | { |
36 | } | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 37 | |
38 | Signature(const Block& info, const Block& value = Block()) | ||||
39 | : m_value(value) | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 41 | setInfo(info); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 42 | } |
43 | |||||
44 | operator bool() const | ||||
45 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 46 | return m_type != -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 47 | } |
48 | |||||
49 | uint32_t | ||||
50 | getType() const | ||||
51 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 52 | return m_type; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 53 | } |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 55 | const Block& |
56 | getInfo() const | ||||
57 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 58 | m_info.encode(); // will do nothing if wire already exists |
59 | return m_info; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 60 | } |
61 | |||||
62 | void | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 63 | setInfo(const Block& info) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 65 | m_info = info; |
66 | if (m_info.hasWire() || m_info.hasValue()) | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 68 | m_info.parse(); |
69 | const Block& signatureType = m_info.get(Tlv::SignatureType); | ||||
70 | m_type = readNonNegativeInteger(signatureType); | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 71 | } |
72 | else | ||||
73 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 74 | m_type = -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 76 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 77 | |
78 | const Block& | ||||
79 | getValue() const | ||||
80 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 81 | m_value.encode(); // will do nothing if wire already exists |
82 | return m_value; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 83 | } |
84 | |||||
85 | void | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 86 | setValue(const Block& value) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 87 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 88 | m_value = value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 89 | } |
90 | |||||
91 | void | ||||
92 | reset() | ||||
93 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 94 | m_type = -1; |
95 | m_info.reset(); | ||||
96 | m_value.reset(); | ||||
97 | } | ||||
98 | |||||
99 | public: // 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 Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 111 | } |
112 | |||||
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 113 | protected: |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 114 | int32_t m_type; |
115 | |||||
116 | mutable Block m_info; | ||||
117 | mutable Block m_value; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 118 | }; |
119 | |||||
120 | } // namespace ndn | ||||
121 | |||||
122 | #endif // NDN_SIGNATURE_HPP |