blob: 1c7a252a61966fb3e6a42874be76493e8308172c [file] [log] [blame]
Junxiao Shi0eba1aa2014-10-25 09:54:08 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev73e30042015-09-17 17:09:51 -07003 * Copyright (c) 2013-2015 Regents of the University of California.
Junxiao Shi0eba1aa2014-10-25 09:54:08 -07004 *
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 Shic6acc7a2015-06-23 10:03:56 -070024#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 Shi0eba1aa2014-10-25 09:54:08 -070029namespace ndn {
30namespace nfd {
31
32const time::milliseconds CommandOptions::DEFAULT_TIMEOUT(10000);
33const Name CommandOptions::DEFAULT_PREFIX("ndn:/localhost/nfd");
34
35CommandOptions::CommandOptions()
36 : m_timeout(DEFAULT_TIMEOUT)
37 , m_prefix(DEFAULT_PREFIX)
Junxiao Shi0eba1aa2014-10-25 09:54:08 -070038{
39}
40
41CommandOptions&
42CommandOptions::setTimeout(const time::milliseconds& timeout)
43{
44 if (timeout <= time::milliseconds::zero()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070045 BOOST_THROW_EXCEPTION(std::out_of_range("Timeout must be positive"));
Junxiao Shi0eba1aa2014-10-25 09:54:08 -070046 }
47
48 m_timeout = timeout;
49 return *this;
50}
51
52CommandOptions&
53CommandOptions::setPrefix(const Name& prefix)
54{
55 m_prefix = prefix;
56 return *this;
57}
58
59CommandOptions&
Junxiao Shic6acc7a2015-06-23 10:03:56 -070060CommandOptions::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
68CommandOptions::SigningParamsKind
69CommandOptions::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 Mastorakis0d2ed2e2015-07-27 19:09:12 -070079 BOOST_THROW_EXCEPTION(std::out_of_range("SigningInfo::SignerType is not convertible to "
80 "CommandOptions::SigningParamsKind"));
Junxiao Shic6acc7a2015-06-23 10:03:56 -070081 }
82}
83
84const Name&
85CommandOptions::getSigningIdentity() const
86{
87 BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_ID);
88 return m_signingInfo.getSignerName();
89}
90
91const Name&
92CommandOptions::getSigningCertificate() const
93{
94 BOOST_ASSERT(m_signingInfo.getSignerType() == security::SigningInfo::SIGNER_TYPE_CERT);
95 return m_signingInfo.getSignerName();
96}
97
98CommandOptions&
99CommandOptions::setSigningDefault()
100{
101 m_signingInfo = security::SigningInfo();
102 return *this;
103}
104
105CommandOptions&
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700106CommandOptions::setSigningIdentity(const Name& identityName)
107{
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700108 m_signingInfo = security::signingByIdentity(identityName);
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700109 return *this;
110}
111
Junxiao Shi35020ca2015-07-01 16:46:51 -0700112static security::SigningInfo
113makeSigningInfoFromIdentityCertificate(const Name& certificateName)
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700114{
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 Mastorakis0d2ed2e2015-07-27 19:09:12 -0700118 BOOST_THROW_EXCEPTION(std::invalid_argument("Certificate is invalid"));
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700119 }
120
Junxiao Shi35020ca2015-07-01 16:46:51 -0700121 return security::signingByCertificate(certificateName);
122}
123
124CommandOptions&
125CommandOptions::setSigningCertificate(const Name& certificateName)
126{
127 m_signingInfo = makeSigningInfoFromIdentityCertificate(certificateName);
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700128 return *this;
129}
130
131CommandOptions&
132CommandOptions::setSigningCertificate(const IdentityCertificate& certificate)
133{
Junxiao Shi35020ca2015-07-01 16:46:51 -0700134 m_signingInfo = makeSigningInfoFromIdentityCertificate(certificate.getName());
135 return *this;
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700136}
137
Junxiao Shic6acc7a2015-06-23 10:03:56 -0700138#endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_KEEP_DEPRECATED_SIGNING_PARAMS
139
Junxiao Shi0eba1aa2014-10-25 09:54:08 -0700140} // namespace nfd
141} // namespace ndn