blob: 16c2f95a8a626b544d6555f1096f9b5774aa14dd [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;
Yingdi Yu64c3fb42014-02-26 17:30:04 -080021 bool privateImport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022
Yingdi Yu64c3fb42014-02-26 17:30:04 -080023 po::options_description desc("General Usage\n ndnsec import [-h] [-p] input \nGeneral options");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024 desc.add_options()
25 ("help,h", "produce help message")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080026 ("private,p", "import info contains private key")
27 ("input,i", po::value<std::string>(&input), "input source, stdin if -")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028 ;
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 Yu64c3fb42014-02-26 17:30:04 -080051 if (vm.count("private"))
52 privateImport = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053
Yingdi Yu64c3fb42014-02-26 17:30:04 -080054 if(!privateImport)
55 {
56 std::cerr << "You are trying to import certificate!\nPlease use ndnsec cert-install!" << std::endl;
57 return 1;
58 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080061 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080063 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064
Yingdi Yu64c3fb42014-02-26 17:30:04 -080065 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 Yu8d7468f2014-02-21 14:49:45 -0800106}
107
108#endif //NDNSEC_IMPORT_HPP