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_IMPORT_HPP |
| 9 | #define NDNSEC_IMPORT_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
| 13 | int |
| 14 | ndnsec_import(int argc, char** argv) |
| 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | std::string input; |
| 20 | std::string importPassword; |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 21 | bool privateImport = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 23 | po::options_description desc("General Usage\n ndnsec import [-h] [-p] input \nGeneral options"); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | desc.add_options() |
| 25 | ("help,h", "produce help message") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 26 | ("private,p", "import info contains private key") |
| 27 | ("input,i", po::value<std::string>(&input), "input source, stdin if -") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | ; |
| 29 | |
| 30 | po::positional_options_description p; |
| 31 | p.add("input", 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 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 51 | if (vm.count("private")) |
| 52 | privateImport = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 54 | if(!privateImport) |
| 55 | { |
| 56 | std::cerr << "You are trying to import certificate!\nPlease use ndnsec cert-install!" << std::endl; |
| 57 | return 1; |
| 58 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 59 | else |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 60 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 61 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 62 | { |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 63 | KeyChain keyChain; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 64 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 65 | shared_ptr<SecuredBag> securedBag; |
| 66 | if(input == "-") |
| 67 | securedBag = io::load<SecuredBag>(std::cin); |
| 68 | else |
| 69 | securedBag = io::load<SecuredBag>(input); |
| 70 | |
| 71 | int count = 3; |
| 72 | while(!getPassword(importPassword, "Passphrase for the private key: ")) |
| 73 | { |
| 74 | count--; |
| 75 | if(count <= 0) |
| 76 | { |
| 77 | std::cerr << "ERROR: Fail to get password" << std::endl; |
| 78 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 79 | return 1; |
| 80 | } |
| 81 | } |
| 82 | keyChain.importIdentity(*securedBag, importPassword); |
| 83 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 84 | } |
| 85 | catch(io::Error& e) |
| 86 | { |
| 87 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 88 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 89 | return 1; |
| 90 | } |
| 91 | catch(SecPublicInfo::Error& e) |
| 92 | { |
| 93 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 94 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 95 | return 1; |
| 96 | } |
| 97 | catch(SecTpm::Error& e) |
| 98 | { |
| 99 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 100 | memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size()); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #endif //NDNSEC_IMPORT_HPP |