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