blob: 19ff0d4ce539391f823fd1570ee23a3216a7f5a9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi476200b2017-10-05 12:16:27 +00002/*
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{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 namespace po = boost::program_options;
32
Alexander Afanasyev35109a12017-01-04 15:39:06 -080033 Name identityName;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 std::string output;
35 std::string exportPassword;
36
Junxiao Shi476200b2017-10-05 12:16:27 +000037 po::options_description description("General Usage\n ndnsec export [-h] [-o output] identity \nGeneral options");
Yingdi Yub61f5402014-02-26 17:46:11 -080038 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 ("help,h", "Produce help message")
40 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080041 ("identity,i", po::value<Name>(&identityName), "Identity to export")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ;
43
44 po::positional_options_description p;
45 p.add("identity", 1);
46
47 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070048 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070050 po::notify(vm);
51 }
52 catch (const std::exception& e) {
53 std::cerr << "ERROR: " << e.what() << std::endl;
54 std::cerr << description << std::endl;
55 return 1;
56 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070058 if (vm.count("help") != 0) {
59 std::cerr << description << std::endl;
60 return 0;
61 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070063 if (vm.count("identity") == 0) {
64 std::cerr << "ERROR: identity must be specified" << std::endl;
65 std::cerr << description << std::endl;
66 return 1;
67 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080068
Yingdi Yub61f5402014-02-26 17:46:11 -080069 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 output = "-";
71
Alexander Afanasyev35109a12017-01-04 15:39:06 -080072 try {
73 int count = 3;
74 while (!getPassword(exportPassword, "Passphrase for the private key: ")) {
75 count--;
76 if (count <= 0) {
77 std::cerr << "ERROR: invalid password" << std::endl;
78 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
79 return 1;
80 }
81 }
82
83 security::v2::KeyChain keyChain;
84 security::Identity id = keyChain.getPib().getIdentity(identityName);
85
86 // @TODO export all certificates, selected key pair, selected certificate
87 shared_ptr<security::SafeBag> safeBag = keyChain.exportSafeBag(id.getDefaultKey().getDefaultCertificate(),
88 exportPassword.c_str(), exportPassword.size());
89 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070090
91 if (output == "-")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080092 io::save(*safeBag, std::cout);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070093 else
Alexander Afanasyev35109a12017-01-04 15:39:06 -080094 io::save(*safeBag, output);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070095
96 return 0;
97 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080098 catch (const std::runtime_error& e) {
99 std::cerr << "ERROR: " << e.what() << std::endl;
100 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
101 return 1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700102 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103}
104
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800105} // namespace ndnsec
106} // namespace ndn