Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 80d671f | 2022-06-08 04:04:52 -0400 | [diff] [blame] | 25 | #include <openssl/crypto.h> |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 26 | |
| 27 | #include <cerrno> |
| 28 | #include <cstring> |
| 29 | #include <unistd.h> |
| 30 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 31 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 32 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | int |
| 34 | ndnsec_unlock_tpm(int argc, char** argv) |
| 35 | { |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 36 | std::cerr << "DEPRECATION NOTICE: ndnsec-unlock-tpm is deprecated.\n"; |
| 37 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | namespace po = boost::program_options; |
| 39 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 40 | po::options_description description( |
| 41 | "Usage: ndnsec unlock-tpm [-h]\n" |
| 42 | "\n" |
| 43 | "Options"); |
| 44 | description.add_options() |
| 45 | ("help,h", "produce help message") |
| 46 | ; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
| 48 | po::variables_map vm; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 49 | try { |
| 50 | po::store(po::parse_command_line(argc, argv, description), vm); |
| 51 | po::notify(vm); |
| 52 | } |
| 53 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 54 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 55 | << description << std::endl; |
| 56 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 57 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 58 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 59 | if (vm.count("help") > 0) { |
| 60 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 61 | return 0; |
| 62 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 63 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 64 | #ifdef NDN_CXX_HAVE_GETPASS |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 65 | KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 66 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 67 | char* password = ::getpass("Password to unlock the TPM: "); |
| 68 | if (password == nullptr) { |
| 69 | std::cerr << "ERROR: getpass() failed: " << std::strerror(errno) << std::endl; |
| 70 | return 1; |
| 71 | } |
| 72 | |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 73 | #pragma GCC diagnostic push |
| 74 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 75 | bool isUnlocked = keyChain.getTpm().unlockTpm(password, std::strlen(password)); |
Davide Pesavento | 9b911e9 | 2024-06-23 13:30:30 -0400 | [diff] [blame] | 76 | #pragma GCC diagnostic pop |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 77 | OPENSSL_cleanse(password, std::strlen(password)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 79 | if (isUnlocked) { |
| 80 | std::cerr << "OK: TPM is unlocked" << std::endl; |
| 81 | return 0; |
| 82 | } |
| 83 | else { |
| 84 | std::cerr << "ERROR: TPM is still locked" << std::endl; |
| 85 | return 1; |
| 86 | } |
Alexander Afanasyev | a2ada22 | 2015-01-22 18:34:16 -0800 | [diff] [blame] | 87 | #else |
| 88 | std::cerr << "ERROR: Command not supported on this platform" << std::endl; |
| 89 | return 1; |
| 90 | #endif // NDN_CXX_HAVE_GETPASS |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 93 | } // namespace ndn::ndnsec |