blob: 48296dd15d2b5cb60358ae1947dc6c68b25e5a74 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
24#ifndef NDNSEC_IMPORT_HPP
25#define NDNSEC_IMPORT_HPP
26
27#include "ndnsec-util.hpp"
28
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030ndnsec_import(int argc, char** argv)
31{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
Yingdi Yub61f5402014-02-26 17:46:11 -080035 std::string input("-");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 std::string importPassword;
Yingdi Yub61f5402014-02-26 17:46:11 -080037 bool isPrivateImport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038
Yingdi Yub61f5402014-02-26 17:46:11 -080039 po::options_description description("General Usage\n ndnsec import [-h] [-p] input \nGeneral options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080042 ("private,p", "import info contains private key")
43 ("input,i", po::value<std::string>(&input), "input source, stdin if -")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ;
45
46 po::positional_options_description p;
47 p.add("input", 1);
48
49 po::variables_map vm;
50 try
51 {
Yingdi Yub61f5402014-02-26 17:46:11 -080052 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
53 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 po::notify(vm);
55 }
Yingdi Yub61f5402014-02-26 17:46:11 -080056 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 {
58 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080059 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060 return 1;
61 }
62
Yingdi Yub61f5402014-02-26 17:46:11 -080063 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064 {
Yingdi Yub61f5402014-02-26 17:46:11 -080065 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066 return 0;
67 }
68
Yingdi Yub61f5402014-02-26 17:46:11 -080069 if (vm.count("private") != 0)
70 isPrivateImport = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 if (!isPrivateImport)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080073 {
Yingdi Yub61f5402014-02-26 17:46:11 -080074 std::cerr << "You are trying to import certificate!\n"
75 << "Please use ndnsec cert-install!" << std::endl;
Yingdi Yu64c3fb42014-02-26 17:30:04 -080076 return 1;
77 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080080 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080082 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083
Yingdi Yu64c3fb42014-02-26 17:30:04 -080084 shared_ptr<SecuredBag> securedBag;
Yingdi Yub61f5402014-02-26 17:46:11 -080085 if (input == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080086 securedBag = io::load<SecuredBag>(std::cin);
87 else
88 securedBag = io::load<SecuredBag>(input);
Yingdi Yub61f5402014-02-26 17:46:11 -080089
Yingdi Yu64c3fb42014-02-26 17:30:04 -080090 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -080091 while (!getPassword(importPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -080092 {
93 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -080094 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080095 {
96 std::cerr << "ERROR: Fail to get password" << std::endl;
97 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
98 return 1;
99 }
100 }
101 keyChain.importIdentity(*securedBag, importPassword);
102 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
103 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800104 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800105 {
106 std::cerr << "ERROR: " << e.what() << std::endl;
107 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
108 return 1;
109 }
110
111 return 0;
112 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800113}
114
115#endif //NDNSEC_IMPORT_HPP