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; |
| 22 | |
| 23 | po::options_description desc("General options"); |
| 24 | desc.add_options() |
| 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); |
| 31 | |
| 32 | po::variables_map vm; |
| 33 | try |
| 34 | { |
| 35 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 36 | po::notify(vm); |
| 37 | } |
| 38 | catch(std::exception &e) |
| 39 | { |
| 40 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if (vm.count("help")) |
| 45 | { |
| 46 | std::cerr << desc << std::endl; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | if (0 == vm.count("command")) |
| 51 | { |
| 52 | std::cerr << "command must be specified" << std::endl; |
| 53 | std::cerr << desc << std::endl; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | if (command == "sign") // the content to be signed from stdin |
| 58 | { |
| 59 | try |
| 60 | { |
| 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) |
| 69 | { |
| 70 | std::cerr << "Error signing with default key" << std::endl; |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | std::cout.write(reinterpret_cast<const char*>(signature.getValue().wire()), signature.getValue().size()); |
| 75 | } |
| 76 | catch (boost::exception& e) |
| 77 | { |
| 78 | std::cerr << "ERROR: " << boost::diagnostic_information (e) << std::endl; |
| 79 | return -1; |
| 80 | } |
| 81 | catch (SecTpm::Error& e) |
| 82 | { |
| 83 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 84 | return -1; |
| 85 | } |
| 86 | catch (SecPublicInfo::Error& e) |
| 87 | { |
| 88 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 89 | return -1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | #endif //NDNSEC_OP_TOOL_HPP |