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 | { |
| 34 | int result = false; |
| 35 | |
| 36 | char* pw0 = NULL; |
| 37 | |
| 38 | pw0 = getpass(prompt.c_str()); |
| 39 | if(!pw0) |
| 40 | return false; |
| 41 | std::string password1 = pw0; |
| 42 | memset(pw0, 0, strlen(pw0)); |
| 43 | |
| 44 | pw0 = getpass("Confirm:"); |
| 45 | if(!pw0) |
| 46 | { |
| 47 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 48 | memset(pw1, 0, password1.size()); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if(!password1.compare(pw0)) |
| 53 | { |
| 54 | result = true; |
| 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 | |
| 62 | if(password.empty()) |
| 63 | return false; |
| 64 | |
| 65 | return result; |
| 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 | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame^] | 72 | if(fileName == "-") |
| 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 |