Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 24 | #include "ndn-cxx/security/impl/openssl.hpp" |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/util/io.hpp" |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace ndnsec { |
| 31 | |
| 32 | bool |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 33 | getPassword(std::string& password, const std::string& prompt, bool shouldConfirm) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 34 | { |
| 35 | #ifdef NDN_CXX_HAVE_GETPASS |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 36 | char* pw0 = getpass(prompt.c_str()); |
| 37 | if (!pw0 || strlen(pw0) == 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | return false; |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 39 | } |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | std::string password1 = pw0; |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 41 | OPENSSL_cleanse(pw0, strlen(pw0)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 43 | if (!shouldConfirm) { |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 44 | password.swap(password1); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | |
Davide Pesavento | fa995ac | 2019-03-27 23:44:46 -0400 | [diff] [blame] | 48 | pw0 = getpass("Confirm: "); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 49 | if (!pw0) { |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 50 | OPENSSL_cleanse(&password1.front(), password1.size()); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 54 | bool isReady = false; |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 55 | if (password1.size() == strlen(pw0) && |
| 56 | CRYPTO_memcmp(password1.data(), pw0, password1.size()) == 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 57 | isReady = true; |
| 58 | password.swap(password1); |
| 59 | } |
Alexander Afanasyev | 22ee089 | 2017-09-02 12:29:16 -0400 | [diff] [blame] | 60 | else { |
| 61 | OPENSSL_cleanse(&password1.front(), password1.size()); |
| 62 | } |
| 63 | OPENSSL_cleanse(pw0, strlen(pw0)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 64 | |
| 65 | return isReady; |
| 66 | #else |
| 67 | return false; |
| 68 | #endif // NDN_CXX_HAVE_GETPASS |
| 69 | } |
| 70 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 71 | security::v2::Certificate |
Junxiao Shi | bc2e78e | 2020-05-20 15:01:08 -0600 | [diff] [blame] | 72 | getCertificateFromPib(const security::pib::Pib& pib, const Name& name, |
| 73 | bool isIdentityName, bool isKeyName, bool isCertName) |
| 74 | { |
| 75 | if (isIdentityName) { |
| 76 | return pib.getIdentity(name) |
| 77 | .getDefaultKey() |
| 78 | .getDefaultCertificate(); |
| 79 | } |
| 80 | else if (isKeyName) { |
| 81 | return pib.getIdentity(security::v2::extractIdentityFromKeyName(name)) |
| 82 | .getKey(name) |
| 83 | .getDefaultCertificate(); |
| 84 | } |
| 85 | else if (isCertName) { |
| 86 | return pib.getIdentity(security::v2::extractIdentityFromCertName(name)) |
| 87 | .getKey(security::v2::extractKeyNameFromCertName(name)) |
| 88 | .getCertificate(name); |
| 89 | } |
| 90 | NDN_CXX_UNREACHABLE; |
| 91 | } |
| 92 | |
| 93 | security::v2::Certificate |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 94 | loadCertificate(const std::string& fileName) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 95 | { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 96 | shared_ptr<security::v2::Certificate> cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 97 | if (fileName == "-") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 98 | cert = io::load<security::v2::Certificate>(std::cin); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 99 | else |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 100 | cert = io::load<security::v2::Certificate>(fileName); |
| 101 | |
| 102 | if (cert == nullptr) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 103 | NDN_THROW(CannotLoadCertificate(fileName)); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 104 | } |
| 105 | return *cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace ndnsec |
| 109 | } // namespace ndn |