Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | 5d69550 | 2016-02-22 11:27:49 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_OP_TOOL_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_OP_TOOL_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | int |
| 30 | ndnsec_op_tool(int argc, char** argv) |
| 31 | { |
| 32 | using namespace ndn; |
| 33 | namespace po = boost::program_options; |
| 34 | |
| 35 | std::string command; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | |
| 37 | po::options_description description("General options"); |
| 38 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | ("help,h", "produce this help message") |
| 40 | ("command", po::value<std::string>(&command), "command") |
| 41 | ; |
| 42 | |
| 43 | po::positional_options_description p; |
| 44 | p.add("command", 1); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 46 | po::variables_map vm; |
| 47 | try |
| 48 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 50 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 51 | po::notify(vm); |
| 52 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | { |
| 55 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 56 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | return -1; |
| 58 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 59 | |
| 60 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
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 0; |
| 64 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 65 | |
| 66 | if (vm.count("command") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 67 | { |
| 68 | std::cerr << "command must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | if (command == "sign") // the content to be signed from stdin |
| 74 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 75 | KeyChain keyChain; |
| 76 | |
Alexander Afanasyev | 5d69550 | 2016-02-22 11:27:49 -0800 | [diff] [blame^] | 77 | Buffer dataToSign((std::istreambuf_iterator<char>(std::cin)), std::istreambuf_iterator<char>()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 79 | Block value = keyChain.sign(dataToSign.buf(), dataToSign.size(), |
| 80 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, |
| 81 | keyChain.getDefaultCertificateName())); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 82 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 83 | if (value.value_size() == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 84 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 85 | std::cerr << "Error signing with default key" << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 86 | return -1; |
| 87 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 5d69550 | 2016-02-22 11:27:49 -0800 | [diff] [blame^] | 89 | value.encode(); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 90 | std::cout.write(reinterpret_cast<const char*>(value.wire()), value.size()); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 96 | #endif // NDN_TOOLS_NDNSEC_OP_TOOL_HPP |