blob: c7753f3dd1b534cc190ae4cc6f0d07b1f0a296a3 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * BSD license, See the LICENSE file for more information
5 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
8#ifndef NDNSEC_EXPORT_HPP
9#define NDNSEC_EXPORT_HPP
10
11#include "ndnsec-util.hpp"
12
13int
14ndnsec_export(int argc, char** argv)
15{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
19 std::string identityStr;
20 std::string output;
21 std::string exportPassword;
22
23 po::options_description desc("General Usage\n ndnsec export [-h] [-o output] identity \nGeneral options");
24 desc.add_options()
25 ("help,h", "Produce help message")
26 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
27 ("identity,i", po::value<std::string>(&identityStr), "Identity to export")
28 ;
29
30 po::positional_options_description p;
31 p.add("identity", 1);
32
33 po::variables_map vm;
34 try
35 {
36 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
37 po::notify(vm);
38 }
39 catch (std::exception &e)
40 {
41 std::cerr << "ERROR: " << e.what() << std::endl;
42 return 1;
43 }
44
45 if (vm.count("help"))
46 {
47 std::cerr << desc << std::endl;
48 return 0;
49 }
50
51 if (!vm.count("output"))
52 output = "-";
53
54 Block wire;
55 Name identity(identityStr);
56
57 try
58 {
59 KeyChain keyChain;
60
61 int count = 3;
62 while(!getPassword(exportPassword, "Passphrase for the private key: "))
63 {
64 count--;
65 if(count <= 0)
66 {
67 std::cerr << "ERROR: invalid password" << std::endl;
68 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
69 return 1;
70 }
71 }
72 wire = keyChain.exportIdentity(identity, exportPassword);
73 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
74 wire.encode();
75 }
76 catch(Block::Error& e)
77 {
78 std::cerr << "ERROR: " << e.what() << std::endl;
79 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
80 return 1;
81 }
82 catch(SecPublicInfo::Error& e)
83 {
84 std::cerr << "ERROR: " << e.what() << std::endl;
85 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
86 return 1;
87 }
88 catch(SecTpm::Error& e)
89 {
90 std::cerr << "ERROR: " << e.what() << std::endl;
91 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
92 return 1;
93 }
94
95 std::ostream* ofs;
96 std::ostream* ffs = 0;
97 if(output == "-")
98 ofs = &std::cout;
99 else
100 {
101 ofs = new std::ofstream(output.c_str());
102 ffs = ofs;
103 }
104
105 try
106 {
107 using namespace CryptoPP;
108
109 StringSource ss(wire.wire(), wire.size(), true,
110 new Base64Encoder(new FileSink(*ofs), true, 64));
111 if(ffs)
112 delete ffs;
113 ffs = 0;
114 ofs = 0;
115 }
116 catch(CryptoPP::Exception& e)
117 {
118 if(ffs)
119 delete ffs;
120 ffs = 0;
121 ofs = 0;
122
123 std::cerr << "ERROR: " << e.what() << std::endl;
124 return 1;
125 }
126
127 return 0;
128}
129
130#endif //NDNSEC_EXPORT_HPP