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 | { |
| 56 | } |
| 57 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 58 | SigningInfo::SigningInfo(const std::string& signingStr) |
| 59 | { |
Spencer Lee | 48c291b | 2015-12-21 01:49:32 -0700 | [diff] [blame] | 60 | *this = SigningInfo(); |
| 61 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 62 | if (signingStr.empty()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | |
| 66 | size_t pos = signingStr.find(':'); |
| 67 | |
| 68 | if (pos == std::string::npos) { |
| 69 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo")); |
| 70 | } |
| 71 | |
| 72 | std::string scheme = signingStr.substr(0, pos); |
| 73 | std::string nameArg = signingStr.substr(pos + 1); |
| 74 | |
| 75 | if (scheme == "id") { |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 76 | if (nameArg == getDigestSha256Identity().toUri()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 77 | setSha256Signing(); |
| 78 | } |
| 79 | else { |
| 80 | setSigningIdentity(nameArg); |
| 81 | } |
| 82 | } |
| 83 | else if (scheme == "key") { |
| 84 | setSigningKeyName(nameArg); |
| 85 | } |
| 86 | else if (scheme == "cert") { |
| 87 | setSigningCertName(nameArg); |
| 88 | } |
| 89 | else { |
| 90 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme")); |
| 91 | } |
| 92 | } |
| 93 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 94 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 95 | SigningInfo::setSigningIdentity(const Name& identity) |
| 96 | { |
| 97 | m_type = SIGNER_TYPE_ID; |
| 98 | m_name = identity; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 99 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 100 | } |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 101 | |
| 102 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 103 | SigningInfo::setSigningKeyName(const Name& keyName) |
| 104 | { |
| 105 | m_type = SIGNER_TYPE_KEY; |
| 106 | m_name = keyName; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 107 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 110 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 111 | SigningInfo::setSigningCertName(const Name& certificateName) |
| 112 | { |
| 113 | m_type = SIGNER_TYPE_CERT; |
| 114 | m_name = certificateName; |
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 | } |
| 117 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 118 | SigningInfo& |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 119 | SigningInfo::setSha256Signing() |
| 120 | { |
| 121 | m_type = SIGNER_TYPE_SHA256; |
| 122 | m_name.clear(); |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 123 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 124 | } |
| 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 | SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo) |
| 128 | { |
| 129 | m_info = signatureInfo; |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 130 | return *this; |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 133 | std::ostream& |
| 134 | operator<<(std::ostream& os, const SigningInfo& si) |
| 135 | { |
| 136 | switch (si.getSignerType()) { |
| 137 | case SigningInfo::SIGNER_TYPE_NULL: |
| 138 | return os; |
| 139 | case SigningInfo::SIGNER_TYPE_ID: |
| 140 | os << "id:"; |
| 141 | break; |
| 142 | case SigningInfo::SIGNER_TYPE_KEY: |
| 143 | os << "key:"; |
| 144 | break; |
| 145 | case SigningInfo::SIGNER_TYPE_CERT: |
| 146 | os << "cert:"; |
| 147 | break; |
| 148 | case SigningInfo::SIGNER_TYPE_SHA256: |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame^] | 149 | os << "id:" << SigningInfo::getDigestSha256Identity(); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 150 | return os; |
| 151 | default: |
| 152 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type")); |
| 153 | } |
| 154 | |
| 155 | os << si.getSignerName(); |
| 156 | return os; |
| 157 | } |
| 158 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 159 | } // namespace security |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 160 | } // namespace ndn |