Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "util.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndnsec { |
| 26 | |
| 27 | bool |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 28 | getPassword(std::string& password, const std::string& prompt, bool shouldConfirm) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 29 | { |
| 30 | #ifdef NDN_CXX_HAVE_GETPASS |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | char* pw0 = nullptr; |
| 32 | |
| 33 | pw0 = getpass(prompt.c_str()); |
| 34 | if (!pw0) |
| 35 | return false; |
| 36 | std::string password1 = pw0; |
| 37 | memset(pw0, 0, strlen(pw0)); |
| 38 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 39 | if (!shouldConfirm) { |
| 40 | return true; |
| 41 | } |
| 42 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 43 | pw0 = getpass("Confirm:"); |
| 44 | if (!pw0) { |
| 45 | char* pw1 = const_cast<char*>(password1.c_str()); |
| 46 | memset(pw1, 0, password1.size()); |
| 47 | return false; |
| 48 | } |
| 49 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 50 | bool isReady = false; |
| 51 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 52 | if (!password1.compare(pw0)) { |
| 53 | isReady = true; |
| 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 | |
| 61 | if (password.empty()) |
| 62 | return false; |
| 63 | |
| 64 | return isReady; |
| 65 | #else |
| 66 | return false; |
| 67 | #endif // NDN_CXX_HAVE_GETPASS |
| 68 | } |
| 69 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 70 | security::v2::Certificate |
| 71 | loadCertificate(const std::string& fileName) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 72 | { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 73 | shared_ptr<security::v2::Certificate> cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 74 | if (fileName == "-") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 75 | cert = io::load<security::v2::Certificate>(std::cin); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 76 | else |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 77 | cert = io::load<security::v2::Certificate>(fileName); |
| 78 | |
| 79 | if (cert == nullptr) { |
| 80 | BOOST_THROW_EXCEPTION(CannotLoadCertificate(fileName)); |
| 81 | } |
| 82 | return *cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace ndnsec |
| 86 | } // namespace ndn |