blob: f3773ac659fd64ada6aa6d4a740ff31b02bce314 [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
Yingdi Yub61f5402014-02-26 17:46:11 -080013int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080014ndnsec_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 Yub61f5402014-02-26 17:46:11 -080022 bool isPrivateExport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080023
Yingdi Yub61f5402014-02-26 17:46:11 -080024 po::options_description description("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options");
25 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026 ("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 {
Yingdi Yub61f5402014-02-26 17:46:11 -080038 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
39 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 po::notify(vm);
41 }
Yingdi Yub61f5402014-02-26 17:46:11 -080042 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043 {
44 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080045 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 return 1;
47 }
48
Yingdi Yub61f5402014-02-26 17:46:11 -080049 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 {
Yingdi Yub61f5402014-02-26 17:46:11 -080051 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052 return 0;
53 }
54
Yingdi Yub61f5402014-02-26 17:46:11 -080055 if (vm.count("identity") == 0)
56 {
57 std::cerr << "ERROR: identity must be specified" << std::endl;
58 std::cerr << description << std::endl;
59 return 1;
60 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080061
Yingdi Yub61f5402014-02-26 17:46:11 -080062 if (vm.count("private") != 0)
63 isPrivateExport = true;
64
65 if (vm.count("output") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 output = "-";
67
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 Name identity(identityStr);
Yingdi Yub61f5402014-02-26 17:46:11 -080069 if (!isPrivateExport)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 {
Yingdi Yub61f5402014-02-26 17:46:11 -080071 KeyChain keyChain;
72 shared_ptr<IdentityCertificate> cert
73 = keyChain.getCertificate(keyChain.getDefaultCertificateNameForIdentity(identity));
74
75 if (output == "-")
76 io::save(*cert, std::cout);
77 else
78 io::save(*cert, output);
79
80 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082 else
83 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080084 Block wire;
85 try
86 {
87 KeyChain keyChain;
88
89 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -080090 while (!getPassword(exportPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -080091 {
92 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -080093 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080094 {
95 std::cerr << "ERROR: invalid password" << std::endl;
96 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
97 return 1;
98 }
99 }
100 shared_ptr<SecuredBag> securedBag = keyChain.exportIdentity(identity, exportPassword);
101 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
Yingdi Yub61f5402014-02-26 17:46:11 -0800102
103 if (output == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800104 io::save(*securedBag, std::cout);
105 else
106 io::save(*securedBag, output);
107
108 return 0;
109 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800110 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800111 {
112 std::cerr << "ERROR: " << e.what() << std::endl;
113 memset(const_cast<char*>(exportPassword.c_str()), 0, exportPassword.size());
114 return 1;
115 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800116 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800117}
118
119#endif //NDNSEC_EXPORT_HPP