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