Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 13 | using namespace std; |
| 14 | |
| 15 | int |
| 16 | ndnsec_op_tool(int argc, char** argv) |
| 17 | { |
| 18 | using namespace ndn; |
| 19 | namespace po = boost::program_options; |
| 20 | |
| 21 | std::string command; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 22 | |
| 23 | po::options_description description("General options"); |
| 24 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 25 | ("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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 31 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | po::variables_map vm; |
| 33 | try |
| 34 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 35 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 36 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | po::notify(vm); |
| 38 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 39 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 40 | { |
| 41 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 42 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | return -1; |
| 44 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 45 | |
| 46 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 48 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 49 | return 0; |
| 50 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | |
| 52 | if (vm.count("command") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | { |
| 54 | std::cerr << "command must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 55 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | if (command == "sign") // the content to be signed from stdin |
| 60 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 61 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 69 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 70 | std::cerr << "Error signing with default key" << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | return -1; |
| 72 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 73 | |
| 74 | std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()), |
| 75 | signature.getValue().size()); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | #endif //NDNSEC_OP_TOOL_HPP |