blob: 90c3284694cc11ba80b5026db590ba8f02c4533e [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocdcde902017-08-23 15:40:22 -04002/*
Davide Pesaventob310efb2019-04-11 22:10:24 -04003 * Copyright (c) 2013-2019 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080022#ifndef NDN_TOOLS_NDNSEC_UTIL_HPP
23#define NDN_TOOLS_NDNSEC_UTIL_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "ndn-cxx/security/key-chain.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "ndn-cxx/security/v2/additional-description.hpp"
27#include "ndn-cxx/util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029#include <fstream>
30#include <iostream>
31#include <string>
32
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080033#include <boost/program_options/options_description.hpp>
34#include <boost/program_options/parsers.hpp>
35#include <boost/program_options/variables_map.hpp>
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080036
37namespace ndn {
38namespace ndnsec {
39
Alexander Afanasyev35109a12017-01-04 15:39:06 -080040class CannotLoadCertificate : public std::runtime_error
41{
42public:
43 CannotLoadCertificate(const std::string& msg)
44 : std::runtime_error(msg)
45 {
46 }
47};
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048
Alexander Afanasyev35109a12017-01-04 15:39:06 -080049bool
50getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true);
51
52security::v2::Certificate
53loadCertificate(const std::string& fileName);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080054
55/**
56 * @brief An accumulating option value to handle multiple incrementing options.
57 *
58 * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp
59 * @sa http://benjaminwolsey.de/node/103
60 */
61template<typename T>
62class AccumulatorType : public boost::program_options::value_semantic
63{
64public:
65 explicit
66 AccumulatorType(T* store)
67 : m_store(store)
68 , m_interval(1)
69 , m_default(0)
70 {
71 }
72
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080073 /// @brief Set the default value for this option.
74 AccumulatorType*
75 setDefaultValue(const T& t)
76 {
77 m_default = t;
78 return this;
79 }
80
81 /**
82 * @brief Set the interval for this option.
83 *
84 * Unlike for program_options::value, this specifies a value
85 * to be applied on each occurrence of the option.
86 */
87 AccumulatorType*
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080088 setInterval(const T& t)
89 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080090 m_interval = t;
91 return this;
92 }
93
Alexander Afanasyev80782e02017-01-04 13:16:54 -080094 std::string
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020095 name() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080096 {
97 return std::string();
98 }
99
100 // There are no tokens for an AccumulatorType
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800101 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200102 min_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800103 {
104 return 0;
105 }
106
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800107 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200108 max_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800109 {
110 return 0;
111 }
112
113 // Accumulating from different sources is silly.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800114 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200115 is_composing() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800116 {
117 return false;
118 }
119
120 // Requiring one or more appearances is unlikely.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800121 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200122 is_required() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800123 {
124 return false;
125 }
126
127 /**
128 * @brief Parse options
129 *
130 * Every appearance of the option simply increments the value
131 * There should never be any tokens.
132 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800133 void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800134 parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800135 {
136 if (value_store.empty())
137 value_store = T();
138 boost::any_cast<T&>(value_store) += m_interval;
139 }
140
141 /**
142 * @brief If the option doesn't appear, this is the default value.
143 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800144 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200145 apply_default(boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800146 {
147 value_store = m_default;
148 return true;
149 }
150
151 /**
152 * @brief Notify the user function with the value of the value store.
153 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800154 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200155 notify(const boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800156 {
157 const T* val = boost::any_cast<T>(&value_store);
158 if (m_store)
159 *m_store = *val;
160 }
161
Davide Pesaventocdcde902017-08-23 15:40:22 -0400162#if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800163 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200164 adjacent_tokens_only() const final
Alexander Afanasyevae205252015-08-24 14:08:46 -0700165 {
166 return false;
167 }
Davide Pesaventocdcde902017-08-23 15:40:22 -0400168#endif // (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyevae205252015-08-24 14:08:46 -0700169
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800170private:
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800171 T* m_store;
172 T m_interval;
173 T m_default;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800174};
175
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800176template <typename T>
177AccumulatorType<T>*
178accumulator()
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800179{
180 return new AccumulatorType<T>(0);
181}
182
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800183template <typename T>
184AccumulatorType<T>*
185accumulator(T* store)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800186{
187 return new AccumulatorType<T>(store);
188}
189
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800190} // namespace ndnsec
191} // namespace ndn
192
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800193#endif // NDN_TOOLS_NDNSEC_UTIL_HPP