blob: b73e1cf424d71f95f9cf35e6fc85c6bf8ee8c799 [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_IMPORT_HPP
9#define NDNSEC_IMPORT_HPP
10
11#include "ndnsec-util.hpp"
12
13int
14ndnsec_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;
21
22 po::options_description desc("General Usage\n ndnsec import [-h] input \nGeneral options");
23 desc.add_options()
24 ("help,h", "produce help message")
25 ("input,i", po::value<std::string>(&input), "input source, stdin if not specified")
26 ;
27
28 po::positional_options_description p;
29 p.add("input", 1);
30
31 po::variables_map vm;
32 try
33 {
34 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
35 po::notify(vm);
36 }
37 catch (std::exception &e)
38 {
39 std::cerr << "ERROR: " << e.what() << std::endl;
40 return 1;
41 }
42
43 if (vm.count("help"))
44 {
45 std::cerr << desc << std::endl;
46 return 0;
47 }
48
49 if (!vm.count("input"))
50 input = "-";
51
52 KeyChain keyChain;
53
54 OBufferStream os;
55 std::istream* ifs;
56 if(input == "-")
57 ifs = &std::cin;
58 else
59 ifs = new std::ifstream(input.c_str());
60
61 {
62 using namespace CryptoPP;
63 FileSource ss(*ifs, true, new Base64Decoder(new FileSink(os)));
64 }
65
66 try
67 {
68 Block wire(os.buf());
69
70 int count = 3;
71 while(!getPassword(importPassword, "Passphrase for the private key: "))
72 {
73 count--;
74 if(count <= 0)
75 {
76 std::cerr << "ERROR: Fail to get password" << std::endl;
77 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
78 return 1;
79 }
80 }
81 keyChain.importIdentity(wire, importPassword);
82 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
83 }
84 catch(Block::Error& e)
85 {
86 std::cerr << "ERROR: " << e.what() << std::endl;
87 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
88 return 1;
89 }
90 catch(SecPublicInfo::Error& e)
91 {
92 std::cerr << "ERROR: " << e.what() << std::endl;
93 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
94 return 1;
95 }
96 catch(SecTpm::Error& e)
97 {
98 std::cerr << "ERROR: " << e.what() << std::endl;
99 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
100 return 1;
101 }
102
103 return 0;
104}
105
106#endif //NDNSEC_IMPORT_HPP