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 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 43 | using tlv::Error::Error; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 44 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 46 | Signature() = default; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 47 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 48 | explicit |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 49 | Signature(const Block& info, const Block& value = Block()); |
| 50 | |
| 51 | explicit |
| 52 | Signature(const SignatureInfo& info, const Block& value = Block()); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 53 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 54 | /** @brief Determine whether SignatureInfo is valid |
| 55 | */ |
| 56 | explicit |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | operator bool() const |
| 58 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 59 | return m_info.getSignatureType() != -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 62 | /** @brief Get SignatureInfo |
Zhiyi Zhang | 387e57b | 2016-08-01 18:50:00 -0700 | [diff] [blame] | 63 | */ |
| 64 | const SignatureInfo& |
| 65 | getSignatureInfo() const |
| 66 | { |
| 67 | return m_info; |
| 68 | } |
| 69 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 70 | /** @brief Get SignatureInfo as wire format |
| 71 | */ |
| 72 | const Block& |
| 73 | getInfo() const |
| 74 | { |
| 75 | return m_info.wireEncode(); |
| 76 | } |
| 77 | |
| 78 | /** @brief Decode SignatureInfo from wire format |
| 79 | * @throw tlv::Error decode error |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 81 | void |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 82 | setInfo(const Block& info); |
| 83 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 84 | /** @brief Set SignatureInfo |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 85 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 86 | void |
| 87 | setInfo(const SignatureInfo& info) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 89 | m_info = info; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 91 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 92 | /** @brief Get SignatureValue |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 93 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 94 | const Block& |
| 95 | getValue() const |
| 96 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 97 | return m_value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 100 | /** @brief Set SignatureValue |
| 101 | * @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] | 102 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 103 | void |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 104 | setValue(const Block& value); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 105 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 106 | public: // SignatureInfo fields |
| 107 | /** @brief Get SignatureType |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 108 | * @throw Error signature is invalid |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 109 | */ |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 110 | tlv::SignatureTypeValue |
| 111 | getType() const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 113 | /** @brief Check if KeyLocator exists in SignatureInfo |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 114 | */ |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 115 | bool |
| 116 | hasKeyLocator() const |
| 117 | { |
| 118 | return m_info.hasKeyLocator(); |
| 119 | } |
| 120 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 121 | /** @brief Get KeyLocator |
| 122 | * @throw tlv::Error KeyLocator does not exist in SignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 123 | */ |
| 124 | const KeyLocator& |
| 125 | getKeyLocator() const |
| 126 | { |
| 127 | return m_info.getKeyLocator(); |
| 128 | } |
| 129 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 130 | /** @brief Set KeyLocator |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 131 | */ |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 132 | void |
| 133 | setKeyLocator(const KeyLocator& keyLocator) |
| 134 | { |
| 135 | m_info.setKeyLocator(keyLocator); |
| 136 | } |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 137 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 138 | /** @brief Unset KeyLocator |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 139 | * |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 140 | * @note Subclasses of Signature may provide advisory (non-virtual) override to prevent unsetting |
| 141 | * KeyLocator if it is required by the specification. |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 142 | */ |
| 143 | void |
| 144 | unsetKeyLocator() |
| 145 | { |
| 146 | m_info.unsetKeyLocator(); |
| 147 | } |
| 148 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 149 | protected: |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 150 | SignatureInfo m_info; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 151 | mutable Block m_value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 152 | }; |
| 153 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 154 | bool |
| 155 | operator==(const Signature& lhs, const Signature& rhs); |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 156 | |
| 157 | inline bool |
| 158 | operator!=(const Signature& lhs, const Signature& rhs) |
| 159 | { |
| 160 | return !(lhs == rhs); |
| 161 | } |
| 162 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 163 | } // namespace ndn |
| 164 | |
| 165 | #endif // NDN_SIGNATURE_HPP |