blob: cd0c786e031d85575462842ff24944bff19341df [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_EXPORT_HPP
25#define NDN_TOOLS_NDNSEC_EXPORT_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030ndnsec_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 Yub61f5402014-02-26 17:46:11 -080038 bool isPrivateExport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039
Yingdi Yub61f5402014-02-26 17:46:11 -080040 po::options_description description("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options");
41 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("help,h", "Produce help message")
43 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080044 ("private,p", "export info contains private key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 ("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 Afanasyev2fa59392016-07-29 17:24:23 -070052 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 Yu8d7468f2014-02-21 14:49:45 -080062
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070063 if (vm.count("help") != 0) {
64 std::cerr << description << std::endl;
65 return 0;
66 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070068 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 Yu64c3fb42014-02-26 17:30:04 -080073
Yingdi Yub61f5402014-02-26 17:46:11 -080074 if (vm.count("private") != 0)
75 isPrivateExport = true;
76
77 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 output = "-";
79
Yingdi Yu8d7468f2014-02-21 14:49:45 -080080 Name identity(identityStr);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070081 if (!isPrivateExport) {
82 KeyChain keyChain;
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 {
Yingdi Yub61f5402014-02-26 17:46:11 -080096 KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080097
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070098 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 Yu64c3fb42014-02-26 17:30:04 -0800103 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
104 return 1;
105 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700106 }
107 shared_ptr<SecuredBag> securedBag = keyChain.exportIdentity(identity, exportPassword);
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 Yu8d7468f2014-02-21 14:49:45 -0800116 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700117 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 Yu8d7468f2014-02-21 14:49:45 -0800123}
124
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800125#endif // NDN_TOOLS_NDNSEC_EXPORT_HPP