Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- 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_UTIL_HPP |
| 9 | #define NDNSEC_UTIL_HPP |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <fstream> |
| 13 | #include <string> |
| 14 | #include <cstring> |
| 15 | |
| 16 | #include <boost/program_options/options_description.hpp> |
| 17 | #include <boost/program_options/variables_map.hpp> |
| 18 | #include <boost/program_options/parsers.hpp> |
| 19 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 20 | #include <boost/tokenizer.hpp> |
| 21 | #include <boost/asio.hpp> |
| 22 | #include <boost/exception/all.hpp> |
| 23 | |
| 24 | |
Junxiao Shi | 482ccc5 | 2014-03-31 13:05:24 -0700 | [diff] [blame] | 25 | #include "security/cryptopp.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
| 27 | #include "security/key-chain.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 28 | #include "util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 29 | |
| 30 | bool |
| 31 | getPassword(std::string& password, const std::string& prompt) |
| 32 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 33 | bool isReady = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 35 | char* pw0 = 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 36 | |
| 37 | pw0 = getpass(prompt.c_str()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | return false; |
| 40 | std::string password1 = pw0; |
| 41 | memset(pw0, 0, strlen(pw0)); |
| 42 | |
| 43 | pw0 = getpass("Confirm:"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 44 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 45 | { |
| 46 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 47 | memset(pw1, 0, password1.size()); |
| 48 | return false; |
| 49 | } |
| 50 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | if (!password1.compare(pw0)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 52 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | isReady = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | password.swap(password1); |
| 55 | } |
| 56 | |
| 57 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 58 | memset(pw1, 0, password1.size()); |
| 59 | memset(pw0, 0, strlen(pw0)); |
| 60 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 61 | if (password.empty()) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 62 | return false; |
| 63 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 64 | return isReady; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ndn::shared_ptr<ndn::IdentityCertificate> |
| 68 | getIdentityCertificate(const std::string& fileName) |
| 69 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | if (fileName == "-") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 72 | return ndn::io::load<ndn::IdentityCertificate>(std::cin); |
| 73 | else |
| 74 | return ndn::io::load<ndn::IdentityCertificate>(fileName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | #endif //NDNSEC_UTIL_HPP |