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