blob: aab60aeb94c763b1cdcdbb121a1deaefb0b3f205 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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;
52 try
53 {
Yingdi Yub61f5402014-02-26 17:46:11 -080054 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
55 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 po::notify(vm);
57 }
Yingdi Yub61f5402014-02-26 17:46:11 -080058 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 {
60 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080061 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 return 1;
63 }
64
Yingdi Yub61f5402014-02-26 17:46:11 -080065 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 {
Yingdi Yub61f5402014-02-26 17:46:11 -080067 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 return 0;
69 }
70
Yingdi Yub61f5402014-02-26 17:46:11 -080071 if (vm.count("identity") == 0)
72 {
73 std::cerr << "ERROR: identity must be specified" << std::endl;
74 std::cerr << description << std::endl;
75 return 1;
76 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080077
Yingdi Yub61f5402014-02-26 17:46:11 -080078 if (vm.count("private") != 0)
79 isPrivateExport = true;
80
81 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082 output = "-";
83
Yingdi Yu8d7468f2014-02-21 14:49:45 -080084 Name identity(identityStr);
Yingdi Yub61f5402014-02-26 17:46:11 -080085 if (!isPrivateExport)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 {
Yingdi Yub61f5402014-02-26 17:46:11 -080087 KeyChain keyChain;
88 shared_ptr<IdentityCertificate> cert
89 = keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity));
90
91 if (output == "-")
92 io::save(*cert, std::cout);
93 else
94 io::save(*cert, output);
95
96 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080097 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 else
99 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800100 Block wire;
101 try
102 {
103 KeyChain keyChain;
104
105 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -0800106 while (!getPassword(exportPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800107 {
108 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -0800109 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800110 {
111 std::cerr << "ERROR: invalid password" << std::endl;
112 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
113 return 1;
114 }
115 }
116 shared_ptr<SecuredBag> securedBag = keyChain.exportIdentity(identity, exportPassword);
117 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800118
119 if (output == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800120 io::save(*securedBag, std::cout);
121 else
122 io::save(*securedBag, output);
123
124 return 0;
125 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800126 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800127 {
128 std::cerr << "ERROR: " << e.what() << std::endl;
129 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
130 return 1;
131 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800132 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800133}
134
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800135#endif // NDN_TOOLS_NDNSEC_EXPORT_HPP