Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 13 | int |
| 14 | ndnsec_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 Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 22 | bool privateExport = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 23 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 24 | po::options_description desc("General Usage\n ndnsec export [-h] [-o output] [-p] identity \nGeneral options"); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 25 | 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 Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 28 | ("private,p", "export info contains private key") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | ("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 Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 53 | if (vm.count("private")) |
| 54 | privateExport = true; |
| 55 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | if (!vm.count("output")) |
| 57 | output = "-"; |
| 58 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 59 | Name identity(identityStr); |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 60 | if(!privateExport) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 62 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 63 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 64 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 72 | } |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 73 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 88 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 89 | else |
| 90 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 91 | 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 135 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | #endif //NDNSEC_EXPORT_HPP |