Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 22 | #ifndef NDN_TOOLS_NDNSEC_UTIL_HPP |
| 23 | #define NDN_TOOLS_NDNSEC_UTIL_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 1944281 | 2018-11-23 14:00:04 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 26 | #include <ndn-cxx/security/key-chain.hpp> |
| 27 | #include <ndn-cxx/security/transform.hpp> |
| 28 | #include <ndn-cxx/security/v2/additional-description.hpp> |
| 29 | #include <ndn-cxx/util/io.hpp> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | #include <fstream> |
| 32 | #include <iostream> |
| 33 | #include <string> |
| 34 | |
| 35 | #include <boost/asio.hpp> |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 36 | #include <boost/exception/all.hpp> |
| 37 | #include <boost/program_options/options_description.hpp> |
| 38 | #include <boost/program_options/parsers.hpp> |
| 39 | #include <boost/program_options/variables_map.hpp> |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | |
| 41 | namespace ndn { |
| 42 | namespace ndnsec { |
| 43 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 44 | class CannotLoadCertificate : public std::runtime_error |
| 45 | { |
| 46 | public: |
| 47 | CannotLoadCertificate(const std::string& msg) |
| 48 | : std::runtime_error(msg) |
| 49 | { |
| 50 | } |
| 51 | }; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 53 | bool |
| 54 | getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true); |
| 55 | |
| 56 | security::v2::Certificate |
| 57 | loadCertificate(const std::string& fileName); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @brief An accumulating option value to handle multiple incrementing options. |
| 61 | * |
| 62 | * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp |
| 63 | * @sa http://benjaminwolsey.de/node/103 |
| 64 | */ |
| 65 | template<typename T> |
| 66 | class AccumulatorType : public boost::program_options::value_semantic |
| 67 | { |
| 68 | public: |
| 69 | explicit |
| 70 | AccumulatorType(T* store) |
| 71 | : m_store(store) |
| 72 | , m_interval(1) |
| 73 | , m_default(0) |
| 74 | { |
| 75 | } |
| 76 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 77 | /// @brief Set the default value for this option. |
| 78 | AccumulatorType* |
| 79 | setDefaultValue(const T& t) |
| 80 | { |
| 81 | m_default = t; |
| 82 | return this; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @brief Set the interval for this option. |
| 87 | * |
| 88 | * Unlike for program_options::value, this specifies a value |
| 89 | * to be applied on each occurrence of the option. |
| 90 | */ |
| 91 | AccumulatorType* |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 92 | setInterval(const T& t) |
| 93 | { |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 94 | m_interval = t; |
| 95 | return this; |
| 96 | } |
| 97 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 98 | std::string |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 99 | name() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 100 | { |
| 101 | return std::string(); |
| 102 | } |
| 103 | |
| 104 | // There are no tokens for an AccumulatorType |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 105 | unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 106 | min_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 107 | { |
| 108 | return 0; |
| 109 | } |
| 110 | |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 111 | unsigned |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 112 | max_tokens() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 113 | { |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | // Accumulating from different sources is silly. |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 118 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 119 | is_composing() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | // Requiring one or more appearances is unlikely. |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 125 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 126 | is_required() const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @brief Parse options |
| 133 | * |
| 134 | * Every appearance of the option simply increments the value |
| 135 | * There should never be any tokens. |
| 136 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 137 | void |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 138 | parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 139 | { |
| 140 | if (value_store.empty()) |
| 141 | value_store = T(); |
| 142 | boost::any_cast<T&>(value_store) += m_interval; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @brief If the option doesn't appear, this is the default value. |
| 147 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 148 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 149 | apply_default(boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 150 | { |
| 151 | value_store = m_default; |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @brief Notify the user function with the value of the value store. |
| 157 | */ |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 158 | void |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 159 | notify(const boost::any& value_store) const final |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 160 | { |
| 161 | const T* val = boost::any_cast<T>(&value_store); |
| 162 | if (m_store) |
| 163 | *m_store = *val; |
| 164 | } |
| 165 | |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 166 | #if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500) |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 167 | bool |
Davide Pesavento | aa82eb6 | 2016-04-22 19:08:40 +0200 | [diff] [blame] | 168 | adjacent_tokens_only() const final |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 169 | { |
| 170 | return false; |
| 171 | } |
Davide Pesavento | cdcde90 | 2017-08-23 15:40:22 -0400 | [diff] [blame] | 172 | #endif // (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500) |
Alexander Afanasyev | ae20525 | 2015-08-24 14:08:46 -0700 | [diff] [blame] | 173 | |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 174 | private: |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 175 | T* m_store; |
| 176 | T m_interval; |
| 177 | T m_default; |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 180 | template <typename T> |
| 181 | AccumulatorType<T>* |
| 182 | accumulator() |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 183 | { |
| 184 | return new AccumulatorType<T>(0); |
| 185 | } |
| 186 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 187 | template <typename T> |
| 188 | AccumulatorType<T>* |
| 189 | accumulator(T* store) |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 190 | { |
| 191 | return new AccumulatorType<T>(store); |
| 192 | } |
| 193 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 194 | } // namespace ndnsec |
| 195 | } // namespace ndn |
| 196 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 197 | #endif // NDN_TOOLS_NDNSEC_UTIL_HPP |