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 | |
| 25 | #include <cryptopp/base64.h> |
| 26 | #include <cryptopp/files.h> |
| 27 | |
| 28 | #include "security/key-chain.hpp" |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 29 | #include "util/io.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | |
| 31 | bool |
| 32 | getPassword(std::string& password, const std::string& prompt) |
| 33 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 34 | bool isReady = false; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | char* pw0 = 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | |
| 38 | pw0 = getpass(prompt.c_str()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 39 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 40 | return false; |
| 41 | std::string password1 = pw0; |
| 42 | memset(pw0, 0, strlen(pw0)); |
| 43 | |
| 44 | pw0 = getpass("Confirm:"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 45 | if (!pw0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 46 | { |
| 47 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 48 | memset(pw1, 0, password1.size()); |
| 49 | return false; |
| 50 | } |
| 51 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 52 | if (!password1.compare(pw0)) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 54 | isReady = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | password.swap(password1); |
| 56 | } |
| 57 | |
| 58 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 59 | memset(pw1, 0, password1.size()); |
| 60 | memset(pw0, 0, strlen(pw0)); |
| 61 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 62 | if (password.empty()) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 63 | return false; |
| 64 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 65 | return isReady; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | ndn::shared_ptr<ndn::IdentityCertificate> |
| 69 | getIdentityCertificate(const std::string& fileName) |
| 70 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | if (fileName == "-") |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 73 | return ndn::io::load<ndn::IdentityCertificate>(std::cin); |
| 74 | else |
| 75 | return ndn::io::load<ndn::IdentityCertificate>(fileName); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | #endif //NDNSEC_UTIL_HPP |