Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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" |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 23 | #include "key-chain.hpp" |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | |
| 28 | const Name SigningInfo::EMPTY_NAME; |
| 29 | const SignatureInfo SigningInfo::EMPTY_SIGNATURE_INFO; |
| 30 | |
| 31 | SigningInfo::SigningInfo(SignerType signerType, |
| 32 | const Name& signerName, |
| 33 | const SignatureInfo& signatureInfo) |
| 34 | : m_type(signerType) |
| 35 | , m_name(signerName) |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 36 | , m_digestAlgorithm(DigestAlgorithm::SHA256) |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 37 | , m_info(signatureInfo) |
| 38 | { |
| 39 | } |
| 40 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 41 | SigningInfo::SigningInfo(const std::string& signingStr) |
| 42 | { |
Spencer Lee | 48c291b | 2015-12-21 01:49:32 -0700 | [diff] [blame] | 43 | *this = SigningInfo(); |
| 44 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 45 | if (signingStr.empty()) { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 46 | return; |
| 47 | } |
| 48 | |
| 49 | size_t pos = signingStr.find(':'); |
| 50 | |
| 51 | if (pos == std::string::npos) { |
| 52 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string cannot represent SigningInfo")); |
| 53 | } |
| 54 | |
| 55 | std::string scheme = signingStr.substr(0, pos); |
| 56 | std::string nameArg = signingStr.substr(pos + 1); |
| 57 | |
| 58 | if (scheme == "id") { |
| 59 | if (nameArg == KeyChain::DIGEST_SHA256_IDENTITY.toUri()) { |
| 60 | setSha256Signing(); |
| 61 | } |
| 62 | else { |
| 63 | setSigningIdentity(nameArg); |
| 64 | } |
| 65 | } |
| 66 | else if (scheme == "key") { |
| 67 | setSigningKeyName(nameArg); |
| 68 | } |
| 69 | else if (scheme == "cert") { |
| 70 | setSigningCertName(nameArg); |
| 71 | } |
| 72 | else { |
| 73 | BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid signing string scheme")); |
| 74 | } |
| 75 | } |
| 76 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 77 | void |
| 78 | SigningInfo::setSigningIdentity(const Name& identity) |
| 79 | { |
| 80 | m_type = SIGNER_TYPE_ID; |
| 81 | m_name = identity; |
| 82 | } |
| 83 | void |
| 84 | SigningInfo::setSigningKeyName(const Name& keyName) |
| 85 | { |
| 86 | m_type = SIGNER_TYPE_KEY; |
| 87 | m_name = keyName; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | SigningInfo::setSigningCertName(const Name& certificateName) |
| 92 | { |
| 93 | m_type = SIGNER_TYPE_CERT; |
| 94 | m_name = certificateName; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | SigningInfo::setSha256Signing() |
| 99 | { |
| 100 | m_type = SIGNER_TYPE_SHA256; |
| 101 | m_name.clear(); |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | SigningInfo::setSignatureInfo(const SignatureInfo& signatureInfo) |
| 106 | { |
| 107 | m_info = signatureInfo; |
| 108 | } |
| 109 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 110 | std::ostream& |
| 111 | operator<<(std::ostream& os, const SigningInfo& si) |
| 112 | { |
| 113 | switch (si.getSignerType()) { |
| 114 | case SigningInfo::SIGNER_TYPE_NULL: |
| 115 | return os; |
| 116 | case SigningInfo::SIGNER_TYPE_ID: |
| 117 | os << "id:"; |
| 118 | break; |
| 119 | case SigningInfo::SIGNER_TYPE_KEY: |
| 120 | os << "key:"; |
| 121 | break; |
| 122 | case SigningInfo::SIGNER_TYPE_CERT: |
| 123 | os << "cert:"; |
| 124 | break; |
| 125 | case SigningInfo::SIGNER_TYPE_SHA256: |
| 126 | os << "id:" << KeyChain::DIGEST_SHA256_IDENTITY; |
| 127 | return os; |
| 128 | default: |
| 129 | BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown signer type")); |
| 130 | } |
| 131 | |
| 132 | os << si.getSignerName(); |
| 133 | return os; |
| 134 | } |
| 135 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 136 | } // namespace security |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 137 | } // namespace ndn |