blob: 97cd702141d2f7fe50ae8edb94ef016318c63f7f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventob310efb2019-04-11 22:10:24 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Davide Pesavento80d671f2022-06-08 04:04:52 -040025#include <openssl/crypto.h>
Davide Pesaventob310efb2019-04-11 22:10:24 -040026
27#include <cerrno>
28#include <cstring>
29#include <unistd.h>
30
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080032
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033int
34ndnsec_unlock_tpm(int argc, char** argv)
35{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 namespace po = boost::program_options;
37
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 po::options_description description(
39 "Usage: ndnsec unlock-tpm [-h]\n"
40 "\n"
41 "Options");
42 description.add_options()
43 ("help,h", "produce help message")
44 ;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045
46 po::variables_map vm;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080047 try {
48 po::store(po::parse_command_line(argc, argv, description), vm);
49 po::notify(vm);
50 }
51 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040052 std::cerr << "ERROR: " << e.what() << "\n\n"
53 << description << std::endl;
54 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080055 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056
Davide Pesaventob310efb2019-04-11 22:10:24 -040057 if (vm.count("help") > 0) {
58 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080059 return 0;
60 }
Yingdi Yub61f5402014-02-26 17:46:11 -080061
Davide Pesaventob310efb2019-04-11 22:10:24 -040062#ifdef NDN_CXX_HAVE_GETPASS
Davide Pesaventof2cae612021-03-24 18:47:05 -040063 KeyChain keyChain;
Yingdi Yub61f5402014-02-26 17:46:11 -080064
Davide Pesaventob310efb2019-04-11 22:10:24 -040065 char* password = ::getpass("Password to unlock the TPM: ");
66 if (password == nullptr) {
67 std::cerr << "ERROR: getpass() failed: " << std::strerror(errno) << std::endl;
68 return 1;
69 }
70
71 bool isUnlocked = keyChain.getTpm().unlockTpm(password, std::strlen(password));
72 OPENSSL_cleanse(password, std::strlen(password));
Yingdi Yub61f5402014-02-26 17:46:11 -080073
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 if (isUnlocked) {
75 std::cerr << "OK: TPM is unlocked" << std::endl;
76 return 0;
77 }
78 else {
79 std::cerr << "ERROR: TPM is still locked" << std::endl;
80 return 1;
81 }
Alexander Afanasyeva2ada222015-01-22 18:34:16 -080082#else
83 std::cerr << "ERROR: Command not supported on this platform" << std::endl;
84 return 1;
85#endif // NDN_CXX_HAVE_GETPASS
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086}
87
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040088} // namespace ndn::ndnsec