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