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