blob: afc393a9e1f6e9a0f00ae2cf1efb0c6364368a51 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- 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_UTIL_HPP
9#define NDNSEC_UTIL_HPP
10
11#include <iostream>
12#include <fstream>
13#include <string>
14#include <cstring>
15
16#include <boost/program_options/options_description.hpp>
17#include <boost/program_options/variables_map.hpp>
18#include <boost/program_options/parsers.hpp>
19#include <boost/date_time/posix_time/posix_time.hpp>
20#include <boost/tokenizer.hpp>
21#include <boost/asio.hpp>
22#include <boost/exception/all.hpp>
23
24
Junxiao Shi482ccc52014-03-31 13:05:24 -070025#include "security/cryptopp.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
27#include "security/key-chain.hpp"
Yingdi Yu64c3fb42014-02-26 17:30:04 -080028#include "util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029
30bool
31getPassword(std::string& password, const std::string& prompt)
32{
Yingdi Yub61f5402014-02-26 17:46:11 -080033 bool isReady = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034
Yingdi Yub61f5402014-02-26 17:46:11 -080035 char* pw0 = 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036
37 pw0 = getpass(prompt.c_str());
Yingdi Yub61f5402014-02-26 17:46:11 -080038 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 return false;
40 std::string password1 = pw0;
41 memset(pw0, 0, strlen(pw0));
42
43 pw0 = getpass("Confirm:");
Yingdi Yub61f5402014-02-26 17:46:11 -080044 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 {
46 char* pw1 = const_cast<char*>(password1.c_str());
47 memset(pw1, 0, password1.size());
48 return false;
49 }
50
Yingdi Yub61f5402014-02-26 17:46:11 -080051 if (!password1.compare(pw0))
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052 {
Yingdi Yub61f5402014-02-26 17:46:11 -080053 isReady = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 password.swap(password1);
55 }
56
57 char* pw1 = const_cast<char*>(password1.c_str());
58 memset(pw1, 0, password1.size());
59 memset(pw0, 0, strlen(pw0));
60
Yingdi Yub61f5402014-02-26 17:46:11 -080061 if (password.empty())
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 return false;
63
Yingdi Yub61f5402014-02-26 17:46:11 -080064 return isReady;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065}
66
67ndn::shared_ptr<ndn::IdentityCertificate>
68getIdentityCertificate(const std::string& fileName)
69{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070
Yingdi Yub61f5402014-02-26 17:46:11 -080071 if (fileName == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080072 return ndn::io::load<ndn::IdentityCertificate>(std::cin);
73 else
74 return ndn::io::load<ndn::IdentityCertificate>(fileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075}
76
77#endif //NDNSEC_UTIL_HPP