blob: 7a244d5f8aa3c95dc225256f7e695aa3198997d6 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_TOOLS_NDNSEC_UTIL_HPP
23#define NDN_CXX_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 Pesavento949075a2021-10-17 22:07:07 -040026#include "ndn-cxx/util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080028#include <iostream>
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080029
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030#include <boost/program_options/options_description.hpp>
31#include <boost/program_options/parsers.hpp>
32#include <boost/program_options/variables_map.hpp>
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080033
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035
Junxiao Shibc2e78e2020-05-20 15:01:08 -060036/**
37 * @brief Get certificate of given name from PIB.
38 * @param pib PIB instance, obtained from keyChain.getPib().
39 * @param name identity name, key name, or cert name.
40 * @param isIdentityName interpret @p name as identity name.
41 * @param isKeyName interpret @p name as key name.
42 * @param isCertName interpret @p name as certificate name.
43 * @pre exactly one of @p isIdentityName , @p isKeyName , @p isCertName must be true.
44 * @return a certificate.
45 * @throw std::invalid_argument name is invalid.
46 * @throw Pib::Error certificate does not exist.
47 */
Davide Pesaventof2cae612021-03-24 18:47:05 -040048security::Certificate
Junxiao Shibc2e78e2020-05-20 15:01:08 -060049getCertificateFromPib(const security::pib::Pib& pib, const Name& name,
50 bool isIdentityName, bool isKeyName, bool isCertName);
51
Davide Pesavento949075a2021-10-17 22:07:07 -040052/**
53 * @brief Load a TLV-encoded, base64-armored object from a file named @p filename.
54 */
55template<typename T>
56T
57loadFromFile(const std::string& filename)
Alexander Afanasyev35109a12017-01-04 15:39:06 -080058{
Davide Pesavento949075a2021-10-17 22:07:07 -040059 try {
60 if (filename == "-") {
61 return io::loadTlv<T>(std::cin, io::BASE64);
62 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063
Davide Pesavento949075a2021-10-17 22:07:07 -040064 std::ifstream file(filename);
65 if (!file) {
66 NDN_THROW(std::runtime_error("Cannot open '" + filename + "'"));
67 }
68 return io::loadTlv<T>(file, io::BASE64);
69 }
70 catch (const io::Error& e) {
71 NDN_THROW_NESTED(std::runtime_error("Cannot load '" + filename +
72 "': malformed TLV or not in base64 format (" + e.what() + ")"));
73 }
74}
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080075
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040076bool
77getPassword(std::string& password, const std::string& prompt, bool shouldConfirm = true);
Yingdi Yu3e8b52e2014-11-26 22:05:00 -080078
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040079} // namespace ndn::ndnsec
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080080
Davide Pesavento09904412021-03-24 16:40:53 -040081#endif // NDN_CXX_TOOLS_NDNSEC_UTIL_HPP