blob: 61b6b520b3cf3793f7bb220c4544e4f3af2598bb [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 Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 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
Junxiao Shi160701a2016-08-30 11:35:25 +000025#include "encoding/buffer-stream.hpp"
Junxiao Shi160701a2016-08-30 11:35:25 +000026#include "security/transform.hpp"
Alexander Afanasyev35109a12017-01-04 15:39:06 -080027#include "security/key-chain.hpp"
28#include "security/v2/additional-description.hpp"
Yingdi Yu64c3fb42014-02-26 17:30:04 -080029#include "util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031#include <fstream>
32#include <iostream>
33#include <string>
34
35#include <boost/asio.hpp>
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080036#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 Afanasyev82c359c2017-01-04 14:48:07 -080040
41namespace ndn {
42namespace ndnsec {
43
Alexander Afanasyev35109a12017-01-04 15:39:06 -080044class CannotLoadCertificate : public std::runtime_error
45{
46public:
47 CannotLoadCertificate(const std::string& msg)
48 : std::runtime_error(msg)
49 {
50 }
51};
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052
Alexander Afanasyev35109a12017-01-04 15:39:06 -080053bool
54getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true);
55
56security::v2::Certificate
57loadCertificate(const std::string& fileName);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080058
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 */
65template<typename T>
66class AccumulatorType : public boost::program_options::value_semantic
67{
68public:
69 explicit
70 AccumulatorType(T* store)
71 : m_store(store)
72 , m_interval(1)
73 , m_default(0)
74 {
75 }
76
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080077 /// @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 Afanasyev82c359c2017-01-04 14:48:07 -080092 setInterval(const T& t)
93 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080094 m_interval = t;
95 return this;
96 }
97
Alexander Afanasyev80782e02017-01-04 13:16:54 -080098 std::string
Davide Pesaventoaa82eb62016-04-22 19:08:40 +020099 name() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800100 {
101 return std::string();
102 }
103
104 // There are no tokens for an AccumulatorType
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800105 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200106 min_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800107 {
108 return 0;
109 }
110
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800111 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200112 max_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800113 {
114 return 0;
115 }
116
117 // Accumulating from different sources is silly.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800118 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200119 is_composing() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800120 {
121 return false;
122 }
123
124 // Requiring one or more appearances is unlikely.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800125 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200126 is_required() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800127 {
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 Afanasyev80782e02017-01-04 13:16:54 -0800137 void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800138 parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800139 {
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 Afanasyev80782e02017-01-04 13:16:54 -0800148 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200149 apply_default(boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800150 {
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 Afanasyev80782e02017-01-04 13:16:54 -0800158 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200159 notify(const boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800160 {
161 const T* val = boost::any_cast<T>(&value_store);
162 if (m_store)
163 *m_store = *val;
164 }
165
Davide Pesaventocdcde902017-08-23 15:40:22 -0400166#if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800167 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200168 adjacent_tokens_only() const final
Alexander Afanasyevae205252015-08-24 14:08:46 -0700169 {
170 return false;
171 }
Davide Pesaventocdcde902017-08-23 15:40:22 -0400172#endif // (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyevae205252015-08-24 14:08:46 -0700173
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800174private:
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800175 T* m_store;
176 T m_interval;
177 T m_default;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800178};
179
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800180template <typename T>
181AccumulatorType<T>*
182accumulator()
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800183{
184 return new AccumulatorType<T>(0);
185}
186
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800187template <typename T>
188AccumulatorType<T>*
189accumulator(T* store)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800190{
191 return new AccumulatorType<T>(store);
192}
193
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800194} // namespace ndnsec
195} // namespace ndn
196
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800197#endif // NDN_TOOLS_NDNSEC_UTIL_HPP