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_EXPORT_HPP |
| 9 | #define NDNSEC_EXPORT_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 13 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 14 | ndnsec_export(int argc, char** argv) |
| 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | std::string identityStr; |
| 20 | std::string output; |
| 21 | std::string exportPassword; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 22 | bool isPrivateExport = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 23 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 24 | po::options_description description("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options"); |
| 25 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | ("help,h", "Produce help message") |
| 27 | ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 28 | ("private,p", "export info contains private key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | ("identity,i", po::value<std::string>(&identityStr), "Identity to export") |
| 30 | ; |
| 31 | |
| 32 | po::positional_options_description p; |
| 33 | p.add("identity", 1); |
| 34 | |
| 35 | po::variables_map vm; |
| 36 | try |
| 37 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 39 | vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 40 | po::notify(vm); |
| 41 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 42 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | { |
| 44 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 45 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | if (vm.count("help") != 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 55 | if (vm.count("identity") == 0) |
| 56 | { |
| 57 | std::cerr << "ERROR: identity must be specified" << std::endl; |
| 58 | std::cerr << description << std::endl; |
| 59 | return 1; |
| 60 | } |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 61 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 62 | if (vm.count("private") != 0) |
| 63 | isPrivateExport = true; |
| 64 | |
| 65 | if (vm.count("output") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 66 | output = "-"; |
| 67 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | Name identity(identityStr); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | if (!isPrivateExport) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | KeyChain keyChain; |
| 72 | shared_ptr<IdentityCertificate> cert |
| 73 | = keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity)); |
| 74 | |
| 75 | if (output == "-") |
| 76 | io::save(*cert, std::cout); |
| 77 | else |
| 78 | io::save(*cert, output); |
| 79 | |
| 80 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 81 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 82 | else |
| 83 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 84 | Block wire; |
| 85 | try |
| 86 | { |
| 87 | KeyChain keyChain; |
| 88 | |
| 89 | int count = 3; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 90 | while (!getPassword(exportPassword, "Passphrase for the private key: ")) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 91 | { |
| 92 | count--; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 93 | if (count <= 0) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 94 | { |
| 95 | std::cerr << "ERROR: invalid password" << std::endl; |
| 96 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | shared_ptr<SecuredBag> securedBag = keyChain.exportIdentity(identity, exportPassword); |
| 101 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 102 | |
| 103 | if (output == "-") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 104 | io::save(*securedBag, std::cout); |
| 105 | else |
| 106 | io::save(*securedBag, output); |
| 107 | |
| 108 | return 0; |
| 109 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 110 | catch (const std::runtime_error& e) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 111 | { |
| 112 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 113 | memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size()); |
| 114 | return 1; |
| 115 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 116 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | #endif //NDNSEC_EXPORT_HPP |