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 | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 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 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 22 | #ifndef NDN_CXX_TOOLS_NDNSEC_UTIL_HPP |
| 23 | #define NDN_CXX_TOOLS_NDNSEC_UTIL_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "ndn-cxx/security/key-chain.hpp" |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame^] | 26 | #include "ndn-cxx/util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 28 | #include <iostream> |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | #include <boost/program_options/options_description.hpp> |
| 31 | #include <boost/program_options/parsers.hpp> |
| 32 | #include <boost/program_options/variables_map.hpp> |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 33 | |
| 34 | namespace ndn { |
| 35 | namespace ndnsec { |
| 36 | |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 37 | /** |
| 38 | * @brief Get certificate of given name from PIB. |
| 39 | * @param pib PIB instance, obtained from keyChain.getPib(). |
| 40 | * @param name identity name, key name, or cert name. |
| 41 | * @param isIdentityName interpret @p name as identity name. |
| 42 | * @param isKeyName interpret @p name as key name. |
| 43 | * @param isCertName interpret @p name as certificate name. |
| 44 | * @pre exactly one of @p isIdentityName , @p isKeyName , @p isCertName must be true. |
| 45 | * @return a certificate. |
| 46 | * @throw std::invalid_argument name is invalid. |
| 47 | * @throw Pib::Error certificate does not exist. |
| 48 | */ |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 49 | security::Certificate |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 50 | getCertificateFromPib(const security::pib::Pib& pib, const Name& name, |
| 51 | bool isIdentityName, bool isKeyName, bool isCertName); |
| 52 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame^] | 53 | /** |
| 54 | * @brief Load a TLV-encoded, base64-armored object from a file named @p filename. |
| 55 | */ |
| 56 | template<typename T> |
| 57 | T |
| 58 | loadFromFile(const std::string& filename) |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 59 | { |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame^] | 60 | try { |
| 61 | if (filename == "-") { |
| 62 | return io::loadTlv<T>(std::cin, io::BASE64); |
| 63 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 64 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame^] | 65 | std::ifstream file(filename); |
| 66 | if (!file) { |
| 67 | NDN_THROW(std::runtime_error("Cannot open '" + filename + "'")); |
| 68 | } |
| 69 | return io::loadTlv<T>(file, io::BASE64); |
| 70 | } |
| 71 | catch (const io::Error& e) { |
| 72 | NDN_THROW_NESTED(std::runtime_error("Cannot load '" + filename + |
| 73 | "': malformed TLV or not in base64 format (" + e.what() + ")")); |
| 74 | } |
| 75 | } |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 76 | |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 77 | bool |
| 78 | getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true); |
Yingdi Yu | 3e8b52e | 2014-11-26 22:05:00 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 80 | } // namespace ndnsec |
| 81 | } // namespace ndn |
| 82 | |
Davide Pesavento | 0990441 | 2021-03-24 16:40:53 -0400 | [diff] [blame] | 83 | #endif // NDN_CXX_TOOLS_NDNSEC_UTIL_HPP |