Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <fstream> |
| 13 | |
| 14 | #include <boost/program_options/options_description.hpp> |
| 15 | #include <boost/program_options/variables_map.hpp> |
| 16 | #include <boost/program_options/parsers.hpp> |
| 17 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 18 | #include <boost/regex.hpp> |
| 19 | #include <boost/exception/all.hpp> |
| 20 | |
| 21 | #include "security/key-chain.hpp" |
| 22 | #include "security/signature-sha256-with-rsa.hpp" |
| 23 | |
| 24 | using namespace std; |
| 25 | using namespace ndn; |
| 26 | namespace po = boost::program_options; |
| 27 | |
| 28 | int main(int argc, char** argv) |
| 29 | { |
| 30 | string command; |
| 31 | |
| 32 | po::options_description desc("General options"); |
| 33 | desc.add_options() |
| 34 | ("help,h", "produce this help message") |
| 35 | ("command", po::value<string>(&command), "command") |
| 36 | ; |
| 37 | |
| 38 | po::positional_options_description p; |
| 39 | p.add("command", 1); |
| 40 | |
| 41 | po::variables_map vm; |
| 42 | try |
| 43 | { |
| 44 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 45 | po::notify(vm); |
| 46 | } |
| 47 | catch(std::exception &e) |
| 48 | { |
| 49 | cerr << "ERROR: " << e.what() << endl; |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | if (vm.count("help")) |
| 54 | { |
| 55 | cout << desc << "\n"; |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | if (0 == vm.count("command")) |
| 60 | { |
| 61 | cerr << "command must be specified" << endl; |
| 62 | cerr << desc << endl; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | if (command == "sign") // the content to be signed from stdin |
| 67 | { |
| 68 | KeyChain keyChain; |
| 69 | |
| 70 | try |
| 71 | { |
| 72 | Buffer dataToSign((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>()); |
| 73 | |
| 74 | Signature signature = keyChain.sign(dataToSign.buf(), dataToSign.size(), |
| 75 | keyChain.getDefaultCertificateName()); |
| 76 | |
| 77 | if (signature.getValue().value_size() == 0) |
| 78 | { |
| 79 | cerr << "Error signing with default key" << endl; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | cout.write(reinterpret_cast<const char*>(signature.getValue().wire()), signature.getValue().size()); |
| 84 | } |
| 85 | catch (boost::exception &e) |
| 86 | { |
| 87 | std::cerr << "ERROR: " << boost::diagnostic_information (e) << std::endl; |
| 88 | return -1; |
| 89 | } |
| 90 | catch(std::exception &e) |
| 91 | { |
| 92 | cerr << "ERROR: " << e.what() << endl; |
| 93 | return -1; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |