blob: d9c079e5de1a0897a2eac8701d054aa23c3e003c [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_IMPORT_HPP
16#define NDNSEC_IMPORT_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080021ndnsec_import(int argc, char** argv)
22{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
Yingdi Yub61f5402014-02-26 17:46:11 -080026 std::string input("-");
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027 std::string importPassword;
Yingdi Yub61f5402014-02-26 17:46:11 -080028 bool isPrivateImport = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029
Yingdi Yub61f5402014-02-26 17:46:11 -080030 po::options_description description("General Usage\n ndnsec import [-h] [-p] input \nGeneral options");
31 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 ("help,h", "produce help message")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080033 ("private,p", "import info contains private key")
34 ("input,i", po::value<std::string>(&input), "input source, stdin if -")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 ;
36
37 po::positional_options_description p;
38 p.add("input", 1);
39
40 po::variables_map vm;
41 try
42 {
Yingdi Yub61f5402014-02-26 17:46:11 -080043 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
44 vm);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 po::notify(vm);
46 }
Yingdi Yub61f5402014-02-26 17:46:11 -080047 catch (const std::exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080048 {
49 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080050 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080051 return 1;
52 }
53
Yingdi Yub61f5402014-02-26 17:46:11 -080054 if (vm.count("help") != 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055 {
Yingdi Yub61f5402014-02-26 17:46:11 -080056 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057 return 0;
58 }
59
Yingdi Yub61f5402014-02-26 17:46:11 -080060 if (vm.count("private") != 0)
61 isPrivateImport = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062
Yingdi Yub61f5402014-02-26 17:46:11 -080063 if (!isPrivateImport)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080064 {
Yingdi Yub61f5402014-02-26 17:46:11 -080065 std::cerr << "You are trying to import certificate!\n"
66 << "Please use ndnsec cert-install!" << std::endl;
Yingdi Yu64c3fb42014-02-26 17:30:04 -080067 return 1;
68 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080071 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072 {
Yingdi Yu64c3fb42014-02-26 17:30:04 -080073 KeyChain keyChain;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074
Yingdi Yu64c3fb42014-02-26 17:30:04 -080075 shared_ptr<SecuredBag> securedBag;
Yingdi Yub61f5402014-02-26 17:46:11 -080076 if (input == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080077 securedBag = io::load<SecuredBag>(std::cin);
78 else
79 securedBag = io::load<SecuredBag>(input);
Yingdi Yub61f5402014-02-26 17:46:11 -080080
Yingdi Yu64c3fb42014-02-26 17:30:04 -080081 int count = 3;
Yingdi Yub61f5402014-02-26 17:46:11 -080082 while (!getPassword(importPassword, "Passphrase for the private key: "))
Yingdi Yu64c3fb42014-02-26 17:30:04 -080083 {
84 count--;
Yingdi Yub61f5402014-02-26 17:46:11 -080085 if (count <= 0)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080086 {
87 std::cerr << "ERROR: Fail to get password" << std::endl;
88 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
89 return 1;
90 }
91 }
92 keyChain.importIdentity(*securedBag, importPassword);
93 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
94 }
Yingdi Yub61f5402014-02-26 17:46:11 -080095 catch (const std::runtime_error& e)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080096 {
97 std::cerr << "ERROR: " << e.what() << std::endl;
98 memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
99 return 1;
100 }
101
102 return 0;
103 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800104}
105
106#endif //NDNSEC_IMPORT_HPP