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) |
| 64 | : m_type(SIGNER_TYPE_PIB_ID) |
| 65 | , m_identity(identity) |
| 66 | , m_digestAlgorithm(DigestAlgorithm::SHA256) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | SigningInfo::SigningInfo(const Key& key) |
| 71 | : m_type(SIGNER_TYPE_PIB_KEY) |
| 72 | , m_key(key) |
| 73 | , m_digestAlgorithm(DigestAlgorithm::SHA256) |
| 74 | { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 77 | SigningInfo::SigningInfo(const std::string& signingStr) |
| 78 | { |
Spencer Lee | 48c291b | 2015-12-21 01:49:32 -0700 | [diff] [blame] | 79 | *this = SigningInfo(); |
| 80 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 81 | if (signingStr.empty()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | size_t pos = signingStr.find(':'); |
| 86 | |
| 87 | if (pos == std::string::npos) { |
| 88 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo")); |
| 89 | } |
| 90 | |
| 91 | std::string scheme = signingStr.substr(0, pos); |
| 92 | std::string nameArg = signingStr.substr(pos + 1); |
| 93 | |
| 94 | if (scheme == "id") { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 95 | if (nameArg == getDigestSha256Identity().toUri()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 96 | setSha256Signing(); |
| 97 | } |
| 98 | else { |
| 99 | setSigningIdentity(nameArg); |
| 100 | } |
| 101 | } |
| 102 | else if (scheme == "key") { |
| 103 | setSigningKeyName(nameArg); |
| 104 | } |
| 105 | else if (scheme == "cert") { |
| 106 | setSigningCertName(nameArg); |
| 107 | } |
| 108 | else { |
| 109 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme")); |
| 110 | } |
| 111 | } |
| 112 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 113 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 114 | SigningInfo::setSigningIdentity(const Name& identity) |
| 115 | { |
| 116 | m_type = SIGNER_TYPE_ID; |
| 117 | m_name = identity; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 118 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 119 | } |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 120 | |
| 121 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 122 | SigningInfo::setSigningKeyName(const Name& keyName) |
| 123 | { |
| 124 | m_type = SIGNER_TYPE_KEY; |
| 125 | m_name = keyName; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 126 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 127 | } |
| 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 | SigningInfo::setSigningCertName(const Name& certificateName) |
| 131 | { |
| 132 | m_type = SIGNER_TYPE_CERT; |
| 133 | m_name = certificateName; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 134 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 137 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 138 | SigningInfo::setSha256Signing() |
| 139 | { |
| 140 | m_type = SIGNER_TYPE_SHA256; |
| 141 | m_name.clear(); |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 142 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 145 | SigningInfo& |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 146 | SigningInfo::setPibIdentity(const Identity& identity) |
| 147 | { |
| 148 | m_type = SIGNER_TYPE_PIB_ID; |
| 149 | m_name.clear(); |
| 150 | m_identity = identity; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | SigningInfo& |
| 155 | SigningInfo::setPibKey(const Key& key) |
| 156 | { |
| 157 | m_type = SIGNER_TYPE_PIB_KEY; |
| 158 | m_name.clear(); |
| 159 | m_key = key; |
| 160 | return *this; |
| 161 | } |
| 162 | |
| 163 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 164 | SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo) |
| 165 | { |
| 166 | m_info = signatureInfo; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 167 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 170 | std::ostream& |
| 171 | operator<<(std::ostream& os, const SigningInfo& si) |
| 172 | { |
| 173 | switch (si.getSignerType()) { |
| 174 | case SigningInfo::SIGNER_TYPE_NULL: |
| 175 | return os; |
| 176 | case SigningInfo::SIGNER_TYPE_ID: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 177 | return os << "id:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 178 | case SigningInfo::SIGNER_TYPE_KEY: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 179 | return os << "key:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 180 | case SigningInfo::SIGNER_TYPE_CERT: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 181 | return os << "cert:" << si.getSignerName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 182 | case SigningInfo::SIGNER_TYPE_SHA256: |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 183 | return os << "id:" << SigningInfo::getDigestSha256Identity(); |
| 184 | case SigningInfo::SIGNER_TYPE_PIB_ID: |
| 185 | return os << "id:" << si.getPibIdentity().getName(); |
| 186 | case SigningInfo::SIGNER_TYPE_PIB_KEY: |
| 187 | return os << "key:" << si.getPibKey().getName(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame^] | 190 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type")); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 191 | return os; |
| 192 | } |
| 193 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 194 | } // namespace security |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 195 | } // namespace ndn |