Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SECURITY_SIGNING_INFO_HPP |
| 23 | #define NDN_SECURITY_SIGNING_INFO_HPP |
| 24 | |
| 25 | #include "../name.hpp" |
| 26 | #include "../signature-info.hpp" |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 27 | #include "pib/identity.hpp" |
| 28 | #include "pib/key.hpp" |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 29 | #include "security-common.hpp" |
| 30 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace security { |
| 33 | |
| 34 | /** |
| 35 | * @brief Signing parameters passed to KeyChain |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 36 | * |
| 37 | * A SigningInfo is invalid if the specified identity/key/certificate does not exist, |
| 38 | * or the PIB Identity or Key instance is not valid. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 39 | */ |
| 40 | class SigningInfo |
| 41 | { |
| 42 | public: |
| 43 | class Error : public std::runtime_error |
| 44 | { |
| 45 | public: |
| 46 | explicit |
| 47 | Error(const std::string& what) |
| 48 | : std::runtime_error(what) |
| 49 | { |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | enum SignerType { |
| 54 | /// @brief no signer is specified, use default setting or follow the trust schema |
| 55 | SIGNER_TYPE_NULL = 0, |
| 56 | /// @brief signer is an identity, use its default key and default certificate |
| 57 | SIGNER_TYPE_ID = 1, |
| 58 | /// @brief signer is a key, use its default certificate |
| 59 | SIGNER_TYPE_KEY = 2, |
| 60 | /// @brief signer is a certificate, use it directly |
| 61 | SIGNER_TYPE_CERT = 3, |
| 62 | /// @brief use sha256 digest, no signer needs to be specified |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 63 | SIGNER_TYPE_SHA256 = 4, |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | public: |
| 67 | /** |
| 68 | * @brief Constructor |
| 69 | * |
| 70 | * @param signerType The type of signer |
| 71 | * @param signerName The name of signer; interpretation differs per signerType |
| 72 | * @param signatureInfo A semi-prepared SignatureInfo which contains other information except |
| 73 | * SignatureType and KeyLocator. If SignatureType and KeyLocator are |
| 74 | * specified, they may be overwritten by KeyChain. |
| 75 | */ |
| 76 | explicit |
| 77 | SigningInfo(SignerType signerType = SIGNER_TYPE_NULL, |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 78 | const Name& signerName = getEmptyName(), |
| 79 | const SignatureInfo& signatureInfo = getEmptySignatureInfo()); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 82 | * @brief Create a signingInfo using pib identity; |
| 83 | */ |
| 84 | explicit |
| 85 | SigningInfo(const Identity& identity); |
| 86 | |
| 87 | /** |
| 88 | * @brief Create a signingInfo using pib key; |
| 89 | */ |
| 90 | explicit |
| 91 | SigningInfo(const Key& key); |
| 92 | |
| 93 | /** |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 94 | * @brief Construct SigningInfo from its string representation |
| 95 | * |
| 96 | * @param signingStr The representative signing string for SigningInfo signing method |
| 97 | * |
| 98 | * Structure of the representative string is as follows: |
| 99 | * - default signing: "" (empty string) |
| 100 | * - signing with a default certificate of a default key for the identity: `id:/my-identity` |
| 101 | * - signing with a default certificate of the key: `key:/my-identity/ksk-1` |
| 102 | * - signing with the certificate: `cert:/my-identity/KEY/ksk-1/ID-CERT/%FD%01` |
| 103 | * - signing with sha256 digest: `id:/localhost/identity/digest-sha256` |
| 104 | */ |
| 105 | explicit |
| 106 | SigningInfo(const std::string& signingStr); |
| 107 | |
| 108 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 109 | * @brief Set signer as an identity with name @p identity |
| 110 | * @post Change the signerType to SIGNER_TYPE_ID |
| 111 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 112 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 113 | setSigningIdentity(const Name& identity); |
| 114 | |
| 115 | /** |
| 116 | * @brief Set signer as a key with name @p keyName |
| 117 | * @post Change the signerType to SIGNER_TYPE_KEY |
| 118 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 119 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 120 | setSigningKeyName(const Name& keyName); |
| 121 | |
| 122 | /** |
| 123 | * @brief Set signer as a certificate with name @p certificateName |
| 124 | * @post Change the signerType to SIGNER_TYPE_CERT |
| 125 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 126 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 127 | setSigningCertName(const Name& certificateName); |
| 128 | |
| 129 | /** |
| 130 | * @brief Set Sha256 as the signing method |
| 131 | * @post Reset signerName, also change the signerType to SIGNER_TYPE_SHA256 |
| 132 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 133 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 134 | setSha256Signing(); |
| 135 | |
| 136 | /** |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 137 | * @brief Set signer as a PIB identity handler @p identity |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 138 | * @post Change the signerType to SIGNER_TYPE_ID |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 139 | */ |
| 140 | SigningInfo& |
| 141 | setPibIdentity(const Identity& identity); |
| 142 | |
| 143 | /** |
| 144 | * @brief Set signer as a PIB key handler @p key |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 145 | * @post Change the signerType to SIGNER_TYPE_KEY |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 146 | */ |
| 147 | SigningInfo& |
| 148 | setPibKey(const Key& key); |
| 149 | |
| 150 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 151 | * @return Type of the signer |
| 152 | */ |
| 153 | SignerType |
| 154 | getSignerType() const |
| 155 | { |
| 156 | return m_type; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return Name of signer; interpretation differs per signerType |
| 161 | */ |
| 162 | const Name& |
| 163 | getSignerName() const |
| 164 | { |
| 165 | return m_name; |
| 166 | } |
| 167 | |
| 168 | /** |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 169 | * @pre signerType must be SIGNER_TYPE_ID |
| 170 | * @return the identity handler of signer, or Identity() if getSignerName() should be used |
| 171 | * to find the identity |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 172 | */ |
| 173 | const Identity& |
| 174 | getPibIdentity() const |
| 175 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 176 | BOOST_ASSERT(m_type == SIGNER_TYPE_ID); |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 177 | return m_identity; |
| 178 | } |
| 179 | |
| 180 | /** |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 181 | * @pre signerType must be SIGNER_TYPE_KEY |
| 182 | * @return the key handler of signer, or Key() if getSignerName() should be used to find the key |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 183 | */ |
| 184 | const Key& |
| 185 | getPibKey() const |
| 186 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 187 | BOOST_ASSERT(m_type == SIGNER_TYPE_KEY); |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 188 | return m_key; |
| 189 | } |
| 190 | |
| 191 | /** |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 192 | * @brief Set the digest algorithm for public key operations |
| 193 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 194 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 195 | setDigestAlgorithm(const DigestAlgorithm& algorithm) |
| 196 | { |
| 197 | m_digestAlgorithm = algorithm; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 198 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @return The digest algorithm for public key operations |
| 203 | */ |
| 204 | DigestAlgorithm |
| 205 | getDigestAlgorithm() const |
| 206 | { |
| 207 | return m_digestAlgorithm; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @brief Set a semi-prepared SignatureInfo; |
| 212 | */ |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 213 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 214 | setSignatureInfo(const SignatureInfo& signatureInfo); |
| 215 | |
| 216 | /** |
| 217 | * @return Semi-prepared SignatureInfo |
| 218 | */ |
| 219 | const SignatureInfo& |
| 220 | getSignatureInfo() const |
| 221 | { |
| 222 | return m_info; |
| 223 | } |
| 224 | |
| 225 | public: |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 226 | static const Name& |
| 227 | getEmptyName(); |
| 228 | |
| 229 | static const SignatureInfo& |
| 230 | getEmptySignatureInfo(); |
| 231 | |
| 232 | /** |
| 233 | * @brief A localhost identity to indicate that the signature is generated using SHA-256. |
| 234 | */ |
| 235 | static const Name& |
| 236 | getDigestSha256Identity(); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 237 | |
Nick Gordon | 2a6d45a | 2017-01-25 14:10:44 -0600 | [diff] [blame^] | 238 | bool |
| 239 | operator==(const SigningInfo& rhs) const; |
| 240 | |
| 241 | bool |
| 242 | operator!=(const SigningInfo& rhs) const |
| 243 | { |
| 244 | return !(*this == rhs); |
| 245 | } |
| 246 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 247 | private: |
| 248 | SignerType m_type; |
| 249 | Name m_name; |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 250 | Identity m_identity; |
| 251 | Key m_key; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 252 | DigestAlgorithm m_digestAlgorithm; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 253 | SignatureInfo m_info; |
| 254 | }; |
| 255 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 256 | std::ostream& |
| 257 | operator<<(std::ostream& os, const SigningInfo& si); |
| 258 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 259 | } // namespace security |
| 260 | } // namespace ndn |
| 261 | |
| 262 | #endif // NDN_SECURITY_SIGNING_INFO_HPP |