blob: 5766949429c327844f121e9feb93939f8dc608ad [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev5d695502016-02-22 11:27:49 -08003 * Copyright (c) 2013-2016 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_OP_TOOL_HPP
25#define NDN_TOOLS_NDNSEC_OP_TOOL_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029int
30ndnsec_op_tool(int argc, char** argv)
31{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
35 std::string command;
Yingdi Yub61f5402014-02-26 17:46:11 -080036
37 po::options_description description("General options");
38 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 ("help,h", "produce this help message")
40 ("command", po::value<std::string>(&command), "command")
41 ;
42
43 po::positional_options_description p;
44 p.add("command", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080045
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 po::variables_map vm;
47 try
48 {
Yingdi Yub61f5402014-02-26 17:46:11 -080049 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
50 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051 po::notify(vm);
52 }
Yingdi Yub61f5402014-02-26 17:46:11 -080053 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
55 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080056 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 return -1;
58 }
Yingdi Yub61f5402014-02-26 17:46:11 -080059
60 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 {
Yingdi Yub61f5402014-02-26 17:46:11 -080062 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 return 0;
64 }
Yingdi Yub61f5402014-02-26 17:46:11 -080065
66 if (vm.count("command") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 {
68 std::cerr << "command must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080069 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 return 1;
71 }
72
73 if (command == "sign") // the content to be signed from stdin
74 {
Yingdi Yub61f5402014-02-26 17:46:11 -080075 KeyChain keyChain;
76
Alexander Afanasyev5d695502016-02-22 11:27:49 -080077 Buffer dataToSign((std::istreambuf_iterator<char>(std::cin)), std::istreambuf_iterator<char>());
Yingdi Yub61f5402014-02-26 17:46:11 -080078
Yingdi Yu1b0311c2015-06-10 14:58:47 -070079 Block value = keyChain.sign(dataToSign.buf(), dataToSign.size(),
80 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT,
81 keyChain.getDefaultCertificateName()));
Yingdi Yub61f5402014-02-26 17:46:11 -080082
Yingdi Yu1b0311c2015-06-10 14:58:47 -070083 if (value.value_size() == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080084 {
Yingdi Yub61f5402014-02-26 17:46:11 -080085 std::cerr << "Error signing with default key" << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 return -1;
87 }
Yingdi Yub61f5402014-02-26 17:46:11 -080088
Alexander Afanasyev5d695502016-02-22 11:27:49 -080089 value.encode();
Yingdi Yu1b0311c2015-06-10 14:58:47 -070090 std::cout.write(reinterpret_cast<const char*>(value.wire()), value.size());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091 }
92
93 return 0;
94}
95
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080096#endif // NDN_TOOLS_NDNSEC_OP_TOOL_HPP