blob: 75dec0eeb60f226d437be12f332942e712093155 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_EXPORT_HPP
16#define NDNSEC_EXPORT_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080021ndnsec_export(int argc, char** argv)
22{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
26 std::string identityStr;
27 std::string output;
28 std::string exportPassword;
Yingdi Yub61f5402014-02-26 17:46:11 -080029 bool isPrivateExport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030
Yingdi Yub61f5402014-02-26 17:46:11 -080031 po::options_description description("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options");
32 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 ("help,h", "Produce help message")
34 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080035 ("private,p", "export info contains private key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 ("identity,i", po::value<std::string>(&identityStr), "Identity to export")
37 ;
38
39 po::positional_options_description p;
40 p.add("identity", 1);
41
42 po::variables_map vm;
43 try
44 {
Yingdi Yub61f5402014-02-26 17:46:11 -080045 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
46 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080047 po::notify(vm);
48 }
Yingdi Yub61f5402014-02-26 17:46:11 -080049 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 {
51 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080052 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 return 1;
54 }
55
Yingdi Yub61f5402014-02-26 17:46:11 -080056 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 {
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 return 0;
60 }
61
Yingdi Yub61f5402014-02-26 17:46:11 -080062 if (vm.count("identity") == 0)
63 {
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("private") != 0)
70 isPrivateExport = true;
71
72 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073 output = "-";
74
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075 Name identity(identityStr);
Yingdi Yub61f5402014-02-26 17:46:11 -080076 if (!isPrivateExport)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 {
Yingdi Yub61f5402014-02-26 17:46:11 -080078 KeyChain keyChain;
79 shared_ptr<IdentityCertificate> cert
80 = keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity));
81
82 if (output == "-")
83 io::save(*cert, std::cout);
84 else
85 io::save(*cert, output);
86
87 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080088 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080089 else
90 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080091 Block wire;
92 try
93 {
94 KeyChain keyChain;
95
96 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -080097 while (!getPassword(exportPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -080098 {
99 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -0800100 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800101 {
102 std::cerr << "ERROR: invalid password" << std::endl;
103 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
104 return 1;
105 }
106 }
107 shared_ptr<SecuredBag> securedBag = keyChain.exportIdentity(identity, exportPassword);
108 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800109
110 if (output == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800111 io::save(*securedBag, std::cout);
112 else
113 io::save(*securedBag, output);
114
115 return 0;
116 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800117 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800118 {
119 std::cerr << "ERROR: " << e.what() << std::endl;
120 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
121 return 1;
122 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800123 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800124}
125
126#endif //NDNSEC_EXPORT_HPP