Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 73e3004 | 2015-09-17 17:09:51 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -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 "nfd-command-options.hpp" |
| 23 | |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 24 | #ifdef NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS |
| 25 | #include "../security/identity-certificate.hpp" |
| 26 | #include "../security/signing-helpers.hpp" |
| 27 | #endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS |
| 28 | |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace nfd { |
| 31 | |
| 32 | const time::milliseconds CommandOptions::DEFAULT_TIMEOUT(10000); |
| 33 | const Name CommandOptions::DEFAULT_PREFIX("ndn:/localhost/nfd"); |
| 34 | |
| 35 | CommandOptions::CommandOptions() |
| 36 | : m_timeout(DEFAULT_TIMEOUT) |
| 37 | , m_prefix(DEFAULT_PREFIX) |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | CommandOptions& |
| 42 | CommandOptions::setTimeout(const time::milliseconds& timeout) |
| 43 | { |
| 44 | if (timeout <= time::milliseconds::zero()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 45 | BOOST_THROW_EXCEPTION(std::out_of_range("Timeout must be positive")); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | m_timeout = timeout; |
| 49 | return *this; |
| 50 | } |
| 51 | |
| 52 | CommandOptions& |
| 53 | CommandOptions::setPrefix(const Name& prefix) |
| 54 | { |
| 55 | m_prefix = prefix; |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | CommandOptions& |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 60 | CommandOptions::setSigningInfo(const security::SigningInfo& signingInfo) |
| 61 | { |
| 62 | m_signingInfo = signingInfo; |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | #ifdef NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS |
| 67 | |
| 68 | CommandOptions::SigningParamsKind |
| 69 | CommandOptions::getSigningParamsKind() const |
| 70 | { |
| 71 | switch (m_signingInfo.getSignerType()) { |
| 72 | case security::SigningInfo::SIGNER_TYPE_NULL: |
| 73 | return SIGNING_PARAMS_DEFAULT; |
| 74 | case security::SigningInfo::SIGNER_TYPE_ID: |
| 75 | return SIGNING_PARAMS_IDENTITY; |
| 76 | case security::SigningInfo::SIGNER_TYPE_CERT: |
| 77 | return SIGNING_PARAMS_CERTIFICATE; |
| 78 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 79 | BOOST_THROW_EXCEPTION(std::out_of_range("SigningInfo::SignerType is not convertible to " |
| 80 | "CommandOptions::SigningParamsKind")); |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | const Name& |
| 85 | CommandOptions::getSigningIdentity() const |
| 86 | { |
| 87 | BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_ID); |
| 88 | return m_signingInfo.getSignerName(); |
| 89 | } |
| 90 | |
| 91 | const Name& |
| 92 | CommandOptions::getSigningCertificate() const |
| 93 | { |
| 94 | BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_CERT); |
| 95 | return m_signingInfo.getSignerName(); |
| 96 | } |
| 97 | |
| 98 | CommandOptions& |
| 99 | CommandOptions::setSigningDefault() |
| 100 | { |
| 101 | m_signingInfo = security::SigningInfo(); |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | CommandOptions& |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 106 | CommandOptions::setSigningIdentity(const Name& identityName) |
| 107 | { |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 108 | m_signingInfo = security::signingByIdentity(identityName); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 109 | return *this; |
| 110 | } |
| 111 | |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 112 | static security::SigningInfo |
| 113 | makeSigningInfoFromIdentityCertificate(const Name& certificateName) |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 114 | { |
| 115 | // A valid IdentityCertificate has at least 4 name components, |
| 116 | // as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model. |
| 117 | if (certificateName.size() < 4) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 118 | BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate is invalid")); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 121 | return security::signingByCertificate(certificateName); |
| 122 | } |
| 123 | |
| 124 | CommandOptions& |
| 125 | CommandOptions::setSigningCertificate(const Name& certificateName) |
| 126 | { |
| 127 | m_signingInfo = makeSigningInfoFromIdentityCertificate(certificateName); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 128 | return *this; |
| 129 | } |
| 130 | |
| 131 | CommandOptions& |
| 132 | CommandOptions::setSigningCertificate(const IdentityCertificate& certificate) |
| 133 | { |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 134 | m_signingInfo = makeSigningInfoFromIdentityCertificate(certificate.getName()); |
| 135 | return *this; |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 138 | #endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS |
| 139 | |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 140 | } // namespace nfd |
| 141 | } // namespace ndn |