blob: ca3662fab0bca67fbcc95ca0f558f2c12be8c5fb [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;
Yingdi Yu64c3fb42014-02-26 17:30:04 -080022 bool privateExport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080023
Yingdi Yu64c3fb42014-02-26 17:30:04 -080024 po::options_description desc("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025 desc.add_options()
26 ("help,h", "Produce help message")
27 ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080028 ("private,p", "export info contains private key")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029 ("identity,i", po::value<std::string>(&identityStr), "Identity to export")
30 ;
31
32 po::positional_options_description p;
33 p.add("identity", 1);
34
35 po::variables_map vm;
36 try
37 {
38 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
39 po::notify(vm);
40 }
41 catch (std::exception &e)
42 {
43 std::cerr << "ERROR: " << e.what() << std::endl;
44 return 1;
45 }
46
47 if (vm.count("help"))
48 {
49 std::cerr << desc << std::endl;
50 return 0;
51 }
52
Yingdi Yu64c3fb42014-02-26 17:30:04 -080053 if (vm.count("private"))
54 privateExport = true;
55
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 if (!vm.count("output"))
57 output = "-";
58
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 Name identity(identityStr);
Yingdi Yu64c3fb42014-02-26 17:30:04 -080060 if(!privateExport)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080062 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080064 KeyChain keyChain;
65 shared_ptr<IdentityCertificate> cert = keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity));
66 if(output == "-")
67 io::save(*cert, std::cout);
68 else
69 io::save(*cert, output);
70
71 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080073 catch(SecPublicInfo::Error& e)
74 {
75 std::cerr << "ERROR: " << e.what() << std::endl;
76 return 1;
77 }
78 catch(SecTpm::Error& e)
79 {
80 std::cerr << "ERROR: " << e.what() << std::endl;
81 return 1;
82 }
83 catch(io::Error& e)
84 {
85 std::cerr << "ERROR: " << e.what() << std::endl;
86 return 1;
87 }
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;
97 while(!getPassword(exportPassword, "Passphrase for the private key: "))
98 {
99 count--;
100 if(count <= 0)
101 {
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());
109
110 if(output == "-")
111 io::save(*securedBag, std::cout);
112 else
113 io::save(*securedBag, output);
114
115 return 0;
116 }
117 catch(io::Error& e)
118 {
119 std::cerr << "ERROR: " << e.what() << std::endl;
120 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
121 return 1;
122 }
123 catch(SecPublicInfo::Error& e)
124 {
125 std::cerr << "ERROR: " << e.what() << std::endl;
126 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
127 return 1;
128 }
129 catch(SecTpm::Error& e)
130 {
131 std::cerr << "ERROR: " << e.what() << std::endl;
132 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
133 return 1;
134 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800135 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800136}
137
138#endif //NDNSEC_EXPORT_HPP