blob: cb957185e17b71b970e030a7e31d2639b9e42ae1 [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/*
Alexander Afanasyev80782e02017-01-04 13:16:54 -08003 * Copyright (c) 2013-2017 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>
36#include <boost/date_time/posix_time/posix_time.hpp>
37#include <boost/exception/all.hpp>
38#include <boost/program_options/options_description.hpp>
39#include <boost/program_options/parsers.hpp>
40#include <boost/program_options/variables_map.hpp>
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080041
42namespace ndn {
43namespace ndnsec {
44
Alexander Afanasyev35109a12017-01-04 15:39:06 -080045class CannotLoadCertificate : public std::runtime_error
46{
47public:
48 CannotLoadCertificate(const std::string& msg)
49 : std::runtime_error(msg)
50 {
51 }
52};
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053
Alexander Afanasyev35109a12017-01-04 15:39:06 -080054bool
55getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true);
56
57security::v2::Certificate
58loadCertificate(const std::string& fileName);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080059
60/**
61 * @brief An accumulating option value to handle multiple incrementing options.
62 *
63 * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp
64 * @sa http://benjaminwolsey.de/node/103
65 */
66template<typename T>
67class AccumulatorType : public boost::program_options::value_semantic
68{
69public:
70 explicit
71 AccumulatorType(T* store)
72 : m_store(store)
73 , m_interval(1)
74 , m_default(0)
75 {
76 }
77
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080078 /// @brief Set the default value for this option.
79 AccumulatorType*
80 setDefaultValue(const T& t)
81 {
82 m_default = t;
83 return this;
84 }
85
86 /**
87 * @brief Set the interval for this option.
88 *
89 * Unlike for program_options::value, this specifies a value
90 * to be applied on each occurrence of the option.
91 */
92 AccumulatorType*
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080093 setInterval(const T& t)
94 {
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080095 m_interval = t;
96 return this;
97 }
98
Alexander Afanasyev80782e02017-01-04 13:16:54 -080099 std::string
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200100 name() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800101 {
102 return std::string();
103 }
104
105 // There are no tokens for an AccumulatorType
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800106 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200107 min_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800108 {
109 return 0;
110 }
111
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800112 unsigned
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200113 max_tokens() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800114 {
115 return 0;
116 }
117
118 // Accumulating from different sources is silly.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800119 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200120 is_composing() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800121 {
122 return false;
123 }
124
125 // Requiring one or more appearances is unlikely.
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800126 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200127 is_required() const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800128 {
129 return false;
130 }
131
132 /**
133 * @brief Parse options
134 *
135 * Every appearance of the option simply increments the value
136 * There should never be any tokens.
137 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800138 void
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800139 parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800140 {
141 if (value_store.empty())
142 value_store = T();
143 boost::any_cast<T&>(value_store) += m_interval;
144 }
145
146 /**
147 * @brief If the option doesn't appear, this is the default value.
148 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800149 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200150 apply_default(boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800151 {
152 value_store = m_default;
153 return true;
154 }
155
156 /**
157 * @brief Notify the user function with the value of the value store.
158 */
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800159 void
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200160 notify(const boost::any& value_store) const final
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800161 {
162 const T* val = boost::any_cast<T>(&value_store);
163 if (m_store)
164 *m_store = *val;
165 }
166
Davide Pesaventocdcde902017-08-23 15:40:22 -0400167#if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800168 bool
Davide Pesaventoaa82eb62016-04-22 19:08:40 +0200169 adjacent_tokens_only() const final
Alexander Afanasyevae205252015-08-24 14:08:46 -0700170 {
171 return false;
172 }
Davide Pesaventocdcde902017-08-23 15:40:22 -0400173#endif // (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
Alexander Afanasyevae205252015-08-24 14:08:46 -0700174
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800175private:
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800176 T* m_store;
177 T m_interval;
178 T m_default;
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800179};
180
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800181template <typename T>
182AccumulatorType<T>*
183accumulator()
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800184{
185 return new AccumulatorType<T>(0);
186}
187
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800188template <typename T>
189AccumulatorType<T>*
190accumulator(T* store)
Yingdi Yu3e8b52e2014-11-26 22:05:00 -0800191{
192 return new AccumulatorType<T>(store);
193}
194
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800195} // namespace ndnsec
196} // namespace ndn
197
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800198#endif // NDN_TOOLS_NDNSEC_UTIL_HPP