Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * BSD license, See the LICENSE file for more information |
| 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDNSEC_UNLOCK_TPM_HPP |
| 9 | #define NDNSEC_UNLOCK_TPM_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
| 13 | int |
| 14 | ndnsec_unlock_tpm(int argc, char** argv) |
| 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 19 | std::string keyName; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 21 | po::options_description description("General Usage\n ndnsec unlock-tpm [-h] \nGeneral options"); |
| 22 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 23 | ("help,h", "produce help message") |
| 24 | ; |
| 25 | |
| 26 | po::variables_map vm; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 27 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | try |
| 29 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | po::store(po::parse_command_line(argc, argv, description), vm); |
| 31 | po::notify(vm); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 33 | catch (const std::exception& e) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | { |
| 35 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | return 1; |
| 38 | } |
| 39 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | if (vm.count("help") != 0) |
| 41 | { |
| 42 | std::cerr << description << std::endl; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | bool isUnlocked = false; |
| 47 | |
| 48 | KeyChain keyChain; |
| 49 | |
| 50 | char* password; |
| 51 | password = getpass("Password to unlock the TPM: "); |
| 52 | isUnlocked = keyChain.unlockTpm(password, strlen(password), true); |
| 53 | memset(password, 0, strlen(password)); |
| 54 | |
| 55 | if (isUnlocked) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 56 | { |
| 57 | std::cerr << "OK: TPM is unlocked" << std::endl; |
| 58 | return 0; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | std::cerr << "ERROR: TPM is still locked" << std::endl; |
| 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #endif //NDNSEC_UNLOCK_TPM_HPP |