blob: 9b7a0e9ff2bd546eaece6c7c101f9f78a6be1783 [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 Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yub61f5402014-02-26 17:46:11 -080028int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029ndnsec_export(int argc, char** argv)
30{
31 using namespace ndn;
32 namespace po = boost::program_options;
33
34 std::string identityStr;
35 std::string output;
36 std::string exportPassword;
Yingdi Yub61f5402014-02-26 17:46:11 -080037 bool isPrivateExport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038
Yingdi Yub61f5402014-02-26 17:46:11 -080039 po::options_description description("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "Produce help message")
42 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080043 ("private,p", "export info contains private key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ("identity,i", po::value<std::string>(&identityStr), "Identity to export")
45 ;
46
47 po::positional_options_description p;
48 p.add("identity", 1);
49
50 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070051 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080052 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070053 po::notify(vm);
54 }
55 catch (const std::exception& e) {
56 std::cerr << "ERROR: " << e.what() << std::endl;
57 std::cerr << description << std::endl;
58 return 1;
59 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070061 if (vm.count("help") != 0) {
62 std::cerr << description << std::endl;
63 return 0;
64 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070066 if (vm.count("identity") == 0) {
67 std::cerr << "ERROR: identity must be specified" << std::endl;
68 std::cerr << description << std::endl;
69 return 1;
70 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 if (vm.count("private") != 0)
73 isPrivateExport = true;
74
75 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076 output = "-";
77
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 Name identity(identityStr);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070079 if (!isPrivateExport) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080080 security::v1::KeyChain keyChain;
81 shared_ptr<security::v1::IdentityCertificate> cert =
82 keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070083
84 if (output == "-")
85 io::save(*cert, std::cout);
86 else
87 io::save(*cert, output);
88
89 return 0;
90 }
91 else {
92 Block wire;
93 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094 security::v1::KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080095
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070096 int count = 3;
97 while (!getPassword(exportPassword, "Passphrase for the private key: ")) {
98 count--;
99 if (count <= 0) {
100 std::cerr << "ERROR: invalid password" << std::endl;
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800101 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
102 return 1;
103 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700104 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800105 shared_ptr<security::v1::SecuredBag> securedBag =
106 keyChain.exportIdentity(identity, exportPassword);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700107 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
108
109 if (output == "-")
110 io::save(*securedBag, std::cout);
111 else
112 io::save(*securedBag, output);
113
114 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800115 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700116 catch (const std::runtime_error& e) {
117 std::cerr << "ERROR: " << e.what() << std::endl;
118 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
119 return 1;
120 }
121 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800122}
123
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800124} // namespace ndnsec
125} // namespace ndn