Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SIGNATURE_HPP |
| 23 | #define NDN_SIGNATURE_HPP |
| 24 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 25 | #include "signature-info.hpp" |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 29 | /** @brief Holds SignatureInfo and SignatureValue in a Data packet |
| 30 | * |
| 31 | * A Signature is not a TLV element itself. It collects SignatureInfo and SignatureValue TLV |
| 32 | * elements together for easy access. |
| 33 | * In most cases, an application should use a subclass of Signature such as @p DigestSha256 , @p |
| 34 | * SignatureSha256WithRsa , or @p SignatureSha256WithEcdsa instead of using @p Signature type |
| 35 | * directly. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 37 | class Signature |
| 38 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 39 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 40 | class Error : public tlv::Error |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 45 | : tlv::Error(what) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 49 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 50 | Signature() = default; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 51 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 52 | explicit |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 53 | Signature(const Block& info, const Block& value = Block()); |
| 54 | |
| 55 | explicit |
| 56 | Signature(const SignatureInfo& info, const Block& value = Block()); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 58 | /** @brief Determine whether SignatureInfo is valid |
| 59 | */ |
| 60 | explicit |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 61 | operator bool() const |
| 62 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 63 | return m_info.getSignatureType() != -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 66 | /** @brief Get SignatureInfo |
Zhiyi Zhang | 387e57b | 2016-08-01 18:50:00 -0700 | [diff] [blame] | 67 | */ |
| 68 | const SignatureInfo& |
| 69 | getSignatureInfo() const |
| 70 | { |
| 71 | return m_info; |
| 72 | } |
| 73 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 74 | /** @brief Get SignatureInfo as wire format |
| 75 | */ |
| 76 | const Block& |
| 77 | getInfo() const |
| 78 | { |
| 79 | return m_info.wireEncode(); |
| 80 | } |
| 81 | |
| 82 | /** @brief Decode SignatureInfo from wire format |
| 83 | * @throw tlv::Error decode error |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 84 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 85 | void |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 86 | setInfo(const Block& info); |
| 87 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 88 | /** @brief Set SignatureInfo |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 89 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 90 | void |
| 91 | setInfo(const SignatureInfo& info) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 93 | m_info = info; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 94 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 95 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 96 | /** @brief Get SignatureValue |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 97 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 98 | const Block& |
| 99 | getValue() const |
| 100 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 101 | return m_value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 104 | /** @brief Set SignatureValue |
| 105 | * @throws tlv::Error TLV-TYPE of supplied block is not SignatureValue, or the block does not have TLV-VALUE |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 106 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 107 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 108 | setValue(const Block& value); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 109 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 110 | public: // SignatureInfo fields |
| 111 | /** @brief Get SignatureType |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 112 | * @throw Error signature is invalid |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 113 | */ |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 114 | tlv::SignatureTypeValue |
| 115 | getType() const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 117 | /** @brief Check if KeyLocator exists in SignatureInfo |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 118 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 119 | bool |
| 120 | hasKeyLocator() const |
| 121 | { |
| 122 | return m_info.hasKeyLocator(); |
| 123 | } |
| 124 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 125 | /** @brief Get KeyLocator |
| 126 | * @throw tlv::Error KeyLocator does not exist in SignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 127 | */ |
| 128 | const KeyLocator& |
| 129 | getKeyLocator() const |
| 130 | { |
| 131 | return m_info.getKeyLocator(); |
| 132 | } |
| 133 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 134 | /** @brief Set KeyLocator |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 135 | */ |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 136 | void |
| 137 | setKeyLocator(const KeyLocator& keyLocator) |
| 138 | { |
| 139 | m_info.setKeyLocator(keyLocator); |
| 140 | } |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 141 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 142 | /** @brief Unset KeyLocator |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 143 | * |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 144 | * @note Subclasses of Signature may provide advisory (non-virtual) override to prevent unsetting |
| 145 | * KeyLocator if it is required by the specification. |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 146 | */ |
| 147 | void |
| 148 | unsetKeyLocator() |
| 149 | { |
| 150 | m_info.unsetKeyLocator(); |
| 151 | } |
| 152 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 153 | protected: |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 154 | SignatureInfo m_info; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 155 | mutable Block m_value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 158 | bool |
| 159 | operator==(const Signature& lhs, const Signature& rhs); |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 160 | |
| 161 | inline bool |
| 162 | operator!=(const Signature& lhs, const Signature& rhs) |
| 163 | { |
| 164 | return !(lhs == rhs); |
| 165 | } |
| 166 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 167 | } // namespace ndn |
| 168 | |
| 169 | #endif // NDN_SIGNATURE_HPP |