blob: d811860ebe579effcb09c4ed592a81862ba7ad75 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_OP_TOOL_HPP
16#define NDNSEC_OP_TOOL_HPP
17
18#include "ndnsec-util.hpp"
19
20using namespace std;
21
22int
23ndnsec_op_tool(int argc, char** argv)
24{
25 using namespace ndn;
26 namespace po = boost::program_options;
27
28 std::string command;
Yingdi Yub61f5402014-02-26 17:46:11 -080029
30 po::options_description description("General options");
31 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 ("help,h", "produce this help message")
33 ("command", po::value<std::string>(&command), "command")
34 ;
35
36 po::positional_options_description p;
37 p.add("command", 1);
Yingdi Yub61f5402014-02-26 17:46:11 -080038
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 po::variables_map vm;
40 try
41 {
Yingdi Yub61f5402014-02-26 17:46:11 -080042 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
43 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 po::notify(vm);
45 }
Yingdi Yub61f5402014-02-26 17:46:11 -080046 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 {
48 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080049 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 return -1;
51 }
Yingdi Yub61f5402014-02-26 17:46:11 -080052
53 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 {
Yingdi Yub61f5402014-02-26 17:46:11 -080055 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 return 0;
57 }
Yingdi Yub61f5402014-02-26 17:46:11 -080058
59 if (vm.count("command") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 {
61 std::cerr << "command must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080062 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 return 1;
64 }
65
66 if (command == "sign") // the content to be signed from stdin
67 {
Yingdi Yub61f5402014-02-26 17:46:11 -080068 KeyChain keyChain;
69
70 Buffer dataToSign((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>());
71
72 Signature signature = keyChain.sign(dataToSign.buf(), dataToSign.size(),
73 keyChain.getDefaultCertificateName());
74
75 if (signature.getValue().value_size() == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 {
Yingdi Yub61f5402014-02-26 17:46:11 -080077 std::cerr << "Error signing with default key" << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 return -1;
79 }
Yingdi Yub61f5402014-02-26 17:46:11 -080080
81 std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()),
82 signature.getValue().size());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083 }
84
85 return 0;
86}
87
88#endif //NDNSEC_OP_TOOL_HPP