blob: 4895ed3b18bda337a931083238e74be50470424e [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev22ee0892017-09-02 12:29:16 -04002/*
Davide Pesavento923ba442019-02-12 22:00:38 -05003 * Copyright (c) 2013-2019 Regents of the University of California.
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08004 *
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 Shi24c5a002018-12-12 04:47:15 +000024#include "ndn-cxx/security/impl/openssl.hpp"
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040025
Davide Pesaventofa995ac2019-03-27 23:44:46 -040026#include <unistd.h>
27
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080028namespace ndn {
29namespace ndnsec {
30
31bool
Alexander Afanasyev35109a12017-01-04 15:39:06 -080032getPassword(std::string& password, const std::string& prompt, bool shouldConfirm)
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080033{
34#ifdef NDN_CXX_HAVE_GETPASS
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040035 char* pw0 = getpass(prompt.c_str());
36 if (!pw0 || strlen(pw0) == 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080037 return false;
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040038 }
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039 std::string password1 = pw0;
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040040 OPENSSL_cleanse(pw0, strlen(pw0));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080041
Alexander Afanasyev35109a12017-01-04 15:39:06 -080042 if (!shouldConfirm) {
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040043 password.swap(password1);
Alexander Afanasyev35109a12017-01-04 15:39:06 -080044 return true;
45 }
46
Davide Pesaventofa995ac2019-03-27 23:44:46 -040047 pw0 = getpass("Confirm: ");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080048 if (!pw0) {
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040049 OPENSSL_cleanse(&password1.front(), password1.size());
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 return false;
51 }
52
Alexander Afanasyev35109a12017-01-04 15:39:06 -080053 bool isReady = false;
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040054 if (password1.size() == strlen(pw0) &&
55 CRYPTO_memcmp(password1.data(), pw0, password1.size()) == 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080056 isReady = true;
57 password.swap(password1);
58 }
Alexander Afanasyev22ee0892017-09-02 12:29:16 -040059 else {
60 OPENSSL_cleanse(&password1.front(), password1.size());
61 }
62 OPENSSL_cleanse(pw0, strlen(pw0));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063
64 return isReady;
65#else
66 return false;
67#endif // NDN_CXX_HAVE_GETPASS
68}
69
Alexander Afanasyev35109a12017-01-04 15:39:06 -080070security::v2::Certificate
71loadCertificate(const std::string& fileName)
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080072{
Alexander Afanasyev35109a12017-01-04 15:39:06 -080073 shared_ptr<security::v2::Certificate> cert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 if (fileName == "-")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080075 cert = io::load<security::v2::Certificate>(std::cin);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080076 else
Alexander Afanasyev35109a12017-01-04 15:39:06 -080077 cert = io::load<security::v2::Certificate>(fileName);
78
79 if (cert == nullptr) {
Davide Pesavento923ba442019-02-12 22:00:38 -050080 NDN_THROW(CannotLoadCertificate(fileName));
Alexander Afanasyev35109a12017-01-04 15:39:06 -080081 }
82 return *cert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080083}
84
85} // namespace ndnsec
86} // namespace ndn