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