Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDNSEC_UTIL_HPP |
| 25 | #define NDNSEC_UTIL_HPP |
| 26 | |
| 27 | #include <iostream> |
| 28 | #include <fstream> |
| 29 | #include <string> |
| 30 | #include <cstring> |
| 31 | |
| 32 | #include <boost/program_options/options_description.hpp> |
| 33 | #include <boost/program_options/variables_map.hpp> |
| 34 | #include <boost/program_options/parsers.hpp> |
| 35 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 36 | #include <boost/tokenizer.hpp> |
| 37 | #include <boost/asio.hpp> |
| 38 | #include <boost/exception/all.hpp> |
| 39 | |
| 40 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 41 | #include "security/cryptopp.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 42 | |
| 43 | #include "security/key-chain.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 44 | #include "util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | |
| 46 | bool |
| 47 | getPassword(std::string& password, const std::string& prompt) |
| 48 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | bool isReady = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 50 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | char* pw0 = 0; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 52 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | pw0 = getpass(prompt.c_str()); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 54 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | return false; |
| 56 | std::string password1 = pw0; |
| 57 | memset(pw0, 0, strlen(pw0)); |
| 58 | |
| 59 | pw0 = getpass("Confirm:"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | { |
| 62 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 63 | memset(pw1, 0, password1.size()); |
| 64 | return false; |
| 65 | } |
| 66 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 67 | if (!password1.compare(pw0)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 69 | isReady = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | password.swap(password1); |
| 71 | } |
| 72 | |
| 73 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 74 | memset(pw1, 0, password1.size()); |
| 75 | memset(pw0, 0, strlen(pw0)); |
| 76 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 77 | if (password.empty()) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | return false; |
| 79 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | return isReady; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | ndn::shared_ptr<ndn::IdentityCertificate> |
| 84 | getIdentityCertificate(const std::string& fileName) |
| 85 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 86 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | if (fileName == "-") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 88 | return ndn::io::load<ndn::IdentityCertificate>(std::cin); |
| 89 | else |
| 90 | return ndn::io::load<ndn::IdentityCertificate>(fileName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | #endif //NDNSEC_UTIL_HPP |