Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 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 "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()) { |
| 45 | throw std::out_of_range("timeout must be positive"); |
| 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: |
| 79 | throw std::out_of_range("SigningInfo::SignerType is not convertible to CommandOptions::SigningParamsKind"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const Name& |
| 84 | CommandOptions::getSigningIdentity() const |
| 85 | { |
| 86 | BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_ID); |
| 87 | return m_signingInfo.getSignerName(); |
| 88 | } |
| 89 | |
| 90 | const Name& |
| 91 | CommandOptions::getSigningCertificate() const |
| 92 | { |
| 93 | BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_CERT); |
| 94 | return m_signingInfo.getSignerName(); |
| 95 | } |
| 96 | |
| 97 | CommandOptions& |
| 98 | CommandOptions::setSigningDefault() |
| 99 | { |
| 100 | m_signingInfo = security::SigningInfo(); |
| 101 | return *this; |
| 102 | } |
| 103 | |
| 104 | CommandOptions& |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 105 | CommandOptions::setSigningIdentity(const Name& identityName) |
| 106 | { |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 107 | m_signingInfo = security::signingByIdentity(identityName); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 108 | return *this; |
| 109 | } |
| 110 | |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 111 | static security::SigningInfo |
| 112 | makeSigningInfoFromIdentityCertificate(const Name& certificateName) |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 113 | { |
| 114 | // A valid IdentityCertificate has at least 4 name components, |
| 115 | // as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model. |
| 116 | if (certificateName.size() < 4) { |
| 117 | throw std::invalid_argument("certificate is invalid"); |
| 118 | } |
| 119 | |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 120 | return security::signingByCertificate(certificateName); |
| 121 | } |
| 122 | |
| 123 | CommandOptions& |
| 124 | CommandOptions::setSigningCertificate(const Name& certificateName) |
| 125 | { |
| 126 | m_signingInfo = makeSigningInfoFromIdentityCertificate(certificateName); |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 127 | return *this; |
| 128 | } |
| 129 | |
| 130 | CommandOptions& |
| 131 | CommandOptions::setSigningCertificate(const IdentityCertificate& certificate) |
| 132 | { |
Junxiao Shi | 35020ca | 2015-07-01 16:46:51 -0700 | [diff] [blame] | 133 | m_signingInfo = makeSigningInfoFromIdentityCertificate(certificate.getName()); |
| 134 | return *this; |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Junxiao Shi | c6acc7a | 2015-06-23 10:03:56 -0700 | [diff] [blame] | 137 | #endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS |
| 138 | |
Junxiao Shi | 0eba1aa | 2014-10-25 09:54:08 -0700 | [diff] [blame] | 139 | } // namespace nfd |
| 140 | } // namespace ndn |