blob: f401909f26ce50b729766c219bcd7fc589363ce3 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
24#ifndef NDNSEC_OP_TOOL_HPP
25#define NDNSEC_OP_TOOL_HPP
26
27#include "ndnsec-util.hpp"
28
29using namespace std;
30
31int
32ndnsec_op_tool(int argc, char** argv)
33{
34 using namespace ndn;
35 namespace po = boost::program_options;
36
37 std::string command;
Yingdi Yub61f5402014-02-26 17:46:11 -080038
39 po::options_description description("General options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce this help message")
42 ("command", po::value<std::string>(&command), "command")
43 ;
44
45 po::positional_options_description p;
46 p.add("command", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080047
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048 po::variables_map vm;
49 try
50 {
Yingdi Yub61f5402014-02-26 17:46:11 -080051 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
52 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 po::notify(vm);
54 }
Yingdi Yub61f5402014-02-26 17:46:11 -080055 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 {
57 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 return -1;
60 }
Yingdi Yub61f5402014-02-26 17:46:11 -080061
62 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 {
Yingdi Yub61f5402014-02-26 17:46:11 -080064 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 return 0;
66 }
Yingdi Yub61f5402014-02-26 17:46:11 -080067
68 if (vm.count("command") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 {
70 std::cerr << "command must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080071 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 return 1;
73 }
74
75 if (command == "sign") // the content to be signed from stdin
76 {
Yingdi Yub61f5402014-02-26 17:46:11 -080077 KeyChain keyChain;
78
79 Buffer dataToSign((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>());
80
81 Signature signature = keyChain.sign(dataToSign.buf(), dataToSign.size(),
82 keyChain.getDefaultCertificateName());
83
84 if (signature.getValue().value_size() == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080085 {
Yingdi Yub61f5402014-02-26 17:46:11 -080086 std::cerr << "Error signing with default key" << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087 return -1;
88 }
Yingdi Yub61f5402014-02-26 17:46:11 -080089
90 std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()),
91 signature.getValue().size());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092 }
93
94 return 0;
95}
96
97#endif //NDNSEC_OP_TOOL_HPP