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 | #include "signing-info.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace security { |
| 26 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 27 | const Name& |
| 28 | SigningInfo::getEmptyName() |
| 29 | { |
| 30 | static Name emptyName; |
| 31 | return emptyName; |
| 32 | } |
| 33 | |
| 34 | const SignatureInfo& |
| 35 | SigningInfo::getEmptySignatureInfo() |
| 36 | { |
| 37 | static SignatureInfo emptySignatureInfo; |
| 38 | return emptySignatureInfo; |
| 39 | } |
| 40 | |
| 41 | const Name& |
| 42 | SigningInfo::getDigestSha256Identity() |
| 43 | { |
| 44 | static Name digestSha256Identity("/localhost/identity/digest-sha256"); |
| 45 | return digestSha256Identity; |
| 46 | } |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 47 | |
| 48 | SigningInfo::SigningInfo(SignerType signerType, |
| 49 | const Name& signerName, |
| 50 | const SignatureInfo& signatureInfo) |
| 51 | : m_type(signerType) |
| 52 | , m_name(signerName) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 53 | , m_digestAlgorithm(DigestAlgorithm::SHA256) |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 54 | , m_info(signatureInfo) |
| 55 | { |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 56 | BOOST_ASSERT(signerType == SIGNER_TYPE_NULL || |
| 57 | signerType == SIGNER_TYPE_ID || |
| 58 | signerType == SIGNER_TYPE_KEY || |
| 59 | signerType == SIGNER_TYPE_CERT || |
| 60 | signerType == SIGNER_TYPE_SHA256); |
| 61 | } |
| 62 | |
| 63 | SigningInfo::SigningInfo(const Identity& identity) |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 64 | : SigningInfo(SIGNER_TYPE_NULL) |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 65 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 66 | this->setPibIdentity(identity); |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SigningInfo::SigningInfo(const Key& key) |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 70 | : SigningInfo(SIGNER_TYPE_NULL) |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 71 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 72 | this->setPibKey(key); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 75 | SigningInfo::SigningInfo(const std::string& signingStr) |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 76 | : SigningInfo(SIGNER_TYPE_NULL) |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 77 | { |
| 78 | if (signingStr.empty()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | size_t pos = signingStr.find(':'); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 83 | if (pos == std::string::npos) { |
| 84 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo")); |
| 85 | } |
| 86 | |
| 87 | std::string scheme = signingStr.substr(0, pos); |
| 88 | std::string nameArg = signingStr.substr(pos + 1); |
| 89 | |
| 90 | if (scheme == "id") { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 91 | if (nameArg == getDigestSha256Identity().toUri()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 92 | setSha256Signing(); |
| 93 | } |
| 94 | else { |
| 95 | setSigningIdentity(nameArg); |
| 96 | } |
| 97 | } |
| 98 | else if (scheme == "key") { |
| 99 | setSigningKeyName(nameArg); |
| 100 | } |
| 101 | else if (scheme == "cert") { |
| 102 | setSigningCertName(nameArg); |
| 103 | } |
| 104 | else { |
| 105 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme")); |
| 106 | } |
| 107 | } |
| 108 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 109 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 110 | SigningInfo::setSigningIdentity(const Name& identity) |
| 111 | { |
| 112 | m_type = SIGNER_TYPE_ID; |
| 113 | m_name = identity; |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 114 | m_identity = Identity(); |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 115 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 116 | } |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 117 | |
| 118 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 119 | SigningInfo::setSigningKeyName(const Name& keyName) |
| 120 | { |
| 121 | m_type = SIGNER_TYPE_KEY; |
| 122 | m_name = keyName; |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 123 | m_key = Key(); |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 124 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 127 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 128 | SigningInfo::setSigningCertName(const Name& certificateName) |
| 129 | { |
| 130 | m_type = SIGNER_TYPE_CERT; |
| 131 | m_name = certificateName; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 132 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 135 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 136 | SigningInfo::setSha256Signing() |
| 137 | { |
| 138 | m_type = SIGNER_TYPE_SHA256; |
| 139 | m_name.clear(); |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 140 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 143 | SigningInfo& |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 144 | SigningInfo::setPibIdentity(const Identity& identity) |
| 145 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 146 | m_type = SIGNER_TYPE_ID; |
| 147 | m_name = identity ? identity.getName() : Name(); |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 148 | m_identity = identity; |
| 149 | return *this; |
| 150 | } |
| 151 | |
| 152 | SigningInfo& |
| 153 | SigningInfo::setPibKey(const Key& key) |
| 154 | { |
Junxiao Shi | fe1239a | 2017-01-27 20:36:12 +0000 | [diff] [blame] | 155 | m_type = SIGNER_TYPE_KEY; |
| 156 | m_name = key ? key.getName() : Name(); |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 157 | m_key = key; |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 162 | SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo) |
| 163 | { |
| 164 | m_info = signatureInfo; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 165 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 168 | std::ostream& |
| 169 | operator<<(std::ostream& os, const SigningInfo& si) |
| 170 | { |
| 171 | switch (si.getSignerType()) { |
| 172 | case SigningInfo::SIGNER_TYPE_NULL: |
| 173 | return os; |
| 174 | case SigningInfo::SIGNER_TYPE_ID: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 175 | return os << "id:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 176 | case SigningInfo::SIGNER_TYPE_KEY: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 177 | return os << "key:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 178 | case SigningInfo::SIGNER_TYPE_CERT: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 179 | return os << "cert:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 180 | case SigningInfo::SIGNER_TYPE_SHA256: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 181 | return os << "id:" << SigningInfo::getDigestSha256Identity(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 184 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type")); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 185 | return os; |
| 186 | } |
| 187 | |
Nick Gordon | 2a6d45a | 2017-01-25 14:10:44 -0600 | [diff] [blame] | 188 | bool |
| 189 | SigningInfo::operator==(const SigningInfo& rhs) const |
| 190 | { |
| 191 | return getSignerType() == rhs.getSignerType() && |
| 192 | getSignerName() == rhs.getSignerName() && |
| 193 | getDigestAlgorithm() == rhs.getDigestAlgorithm() && |
| 194 | getSignatureInfo() == rhs.getSignatureInfo(); |
| 195 | } |
| 196 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 197 | } // namespace security |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 198 | } // namespace ndn |