blob: 0dd6b89d0ef3eb9fac257832e39f309a62f0bb3f [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
Yingdi Yub61f5402014-02-26 17:46:11 -080013int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080014ndnsec_import(int argc, char** argv)
15{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
Yingdi Yub61f5402014-02-26 17:46:11 -080019 std::string input("-");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 std::string importPassword;
Yingdi Yub61f5402014-02-26 17:46:11 -080021 bool isPrivateImport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022
Yingdi Yub61f5402014-02-26 17:46:11 -080023 po::options_description description("General Usage\n ndnsec import [-h] [-p] input \nGeneral options");
24 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080025 ("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 {
Yingdi Yub61f5402014-02-26 17:46:11 -080036 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
37 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 po::notify(vm);
39 }
Yingdi Yub61f5402014-02-26 17:46:11 -080040 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 {
42 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080043 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 return 1;
45 }
46
Yingdi Yub61f5402014-02-26 17:46:11 -080047 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048 {
Yingdi Yub61f5402014-02-26 17:46:11 -080049 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080050 return 0;
51 }
52
Yingdi Yub61f5402014-02-26 17:46:11 -080053 if (vm.count("private") != 0)
54 isPrivateImport = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055
Yingdi Yub61f5402014-02-26 17:46:11 -080056 if (!isPrivateImport)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080057 {
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::cerr << "You are trying to import certificate!\n"
59 << "Please use ndnsec cert-install!" << std::endl;
Yingdi Yu64c3fb42014-02-26 17:30:04 -080060 return 1;
61 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080064 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080066 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067
Yingdi Yu64c3fb42014-02-26 17:30:04 -080068 shared_ptr<SecuredBag> securedBag;
Yingdi Yub61f5402014-02-26 17:46:11 -080069 if (input == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080070 securedBag = io::load<SecuredBag>(std::cin);
71 else
72 securedBag = io::load<SecuredBag>(input);
Yingdi Yub61f5402014-02-26 17:46:11 -080073
Yingdi Yu64c3fb42014-02-26 17:30:04 -080074 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -080075 while (!getPassword(importPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -080076 {
77 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -080078 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080079 {
80 std::cerr << "ERROR: Fail to get password" << std::endl;
81 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
82 return 1;
83 }
84 }
85 keyChain.importIdentity(*securedBag, importPassword);
86 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
87 }
Yingdi Yub61f5402014-02-26 17:46:11 -080088 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080089 {
90 std::cerr << "ERROR: " << e.what() << std::endl;
91 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
92 return 1;
93 }
94
95 return 0;
96 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080097}
98
99#endif //NDNSEC_IMPORT_HPP