blob: 0501a063f550def5541f1475b68e889f994966f1 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * BSD license, See the LICENSE file for more information
5 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
8#ifndef NDNSEC_OP_TOOL_HPP
9#define NDNSEC_OP_TOOL_HPP
10
11#include "ndnsec-util.hpp"
12
13using namespace std;
14
15int
16ndnsec_op_tool(int argc, char** argv)
17{
18 using namespace ndn;
19 namespace po = boost::program_options;
20
21 std::string command;
Yingdi Yub61f5402014-02-26 17:46:11 -080022
23 po::options_description description("General options");
24 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025 ("help,h", "produce this help message")
26 ("command", po::value<std::string>(&command), "command")
27 ;
28
29 po::positional_options_description p;
30 p.add("command", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080031
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 po::variables_map vm;
33 try
34 {
Yingdi Yub61f5402014-02-26 17:46:11 -080035 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
36 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 po::notify(vm);
38 }
Yingdi Yub61f5402014-02-26 17:46:11 -080039 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 {
41 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080042 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043 return -1;
44 }
Yingdi Yub61f5402014-02-26 17:46:11 -080045
46 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 {
Yingdi Yub61f5402014-02-26 17:46:11 -080048 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 return 0;
50 }
Yingdi Yub61f5402014-02-26 17:46:11 -080051
52 if (vm.count("command") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 {
54 std::cerr << "command must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080055 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 return 1;
57 }
58
59 if (command == "sign") // the content to be signed from stdin
60 {
Yingdi Yub61f5402014-02-26 17:46:11 -080061 KeyChain keyChain;
62
63 Buffer dataToSign((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>());
64
65 Signature signature = keyChain.sign(dataToSign.buf(), dataToSign.size(),
66 keyChain.getDefaultCertificateName());
67
68 if (signature.getValue().value_size() == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 {
Yingdi Yub61f5402014-02-26 17:46:11 -080070 std::cerr << "Error signing with default key" << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 return -1;
72 }
Yingdi Yub61f5402014-02-26 17:46:11 -080073
74 std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()),
75 signature.getValue().size());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 }
77
78 return 0;
79}
80
81#endif //NDNSEC_OP_TOOL_HPP