Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 2 | /** |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDNSEC_OP_TOOL_HPP |
| 16 | #define NDNSEC_OP_TOOL_HPP |
| 17 | |
| 18 | #include "ndnsec-util.hpp" |
| 19 | |
| 20 | using namespace std; |
| 21 | |
| 22 | int |
| 23 | ndnsec_op_tool(int argc, char** argv) |
| 24 | { |
| 25 | using namespace ndn; |
| 26 | namespace po = boost::program_options; |
| 27 | |
| 28 | std::string command; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | |
| 30 | po::options_description description("General options"); |
| 31 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | ("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 Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | po::variables_map vm; |
| 40 | try |
| 41 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 42 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 43 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | po::notify(vm); |
| 45 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 46 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | { |
| 48 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | return -1; |
| 51 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 52 | |
| 53 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | { |
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 0; |
| 57 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | |
| 59 | if (vm.count("command") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | { |
| 61 | std::cerr << "command must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 62 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | if (command == "sign") // the content to be signed from stdin |
| 67 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 77 | std::cerr << "Error signing with default key" << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | return -1; |
| 79 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | |
| 81 | std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()), |
| 82 | signature.getValue().size()); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | #endif //NDNSEC_OP_TOOL_HPP |