blob: 0934d90d49040f4b25511919eea2e6d866be74a8 [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
25#include <cryptopp/base64.h>
26#include <cryptopp/files.h>
27
28#include "security/key-chain.hpp"
Yingdi Yu64c3fb42014-02-26 17:30:04 -080029#include "util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030
31bool
32getPassword(std::string& password, const std::string& prompt)
33{
Yingdi Yub61f5402014-02-26 17:46:11 -080034 bool isReady = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035
Yingdi Yub61f5402014-02-26 17:46:11 -080036 char* pw0 = 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037
38 pw0 = getpass(prompt.c_str());
Yingdi Yub61f5402014-02-26 17:46:11 -080039 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040 return false;
41 std::string password1 = pw0;
42 memset(pw0, 0, strlen(pw0));
43
44 pw0 = getpass("Confirm:");
Yingdi Yub61f5402014-02-26 17:46:11 -080045 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 {
47 char* pw1 = const_cast<char*>(password1.c_str());
48 memset(pw1, 0, password1.size());
49 return false;
50 }
51
Yingdi Yub61f5402014-02-26 17:46:11 -080052 if (!password1.compare(pw0))
Yingdi Yu8d7468f2014-02-21 14:49:45 -080053 {
Yingdi Yub61f5402014-02-26 17:46:11 -080054 isReady = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055 password.swap(password1);
56 }
57
58 char* pw1 = const_cast<char*>(password1.c_str());
59 memset(pw1, 0, password1.size());
60 memset(pw0, 0, strlen(pw0));
61
Yingdi Yub61f5402014-02-26 17:46:11 -080062 if (password.empty())
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063 return false;
64
Yingdi Yub61f5402014-02-26 17:46:11 -080065 return isReady;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080066}
67
68ndn::shared_ptr<ndn::IdentityCertificate>
69getIdentityCertificate(const std::string& fileName)
70{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 if (fileName == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080073 return ndn::io::load<ndn::IdentityCertificate>(std::cin);
74 else
75 return ndn::io::load<ndn::IdentityCertificate>(fileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076}
77
78#endif //NDNSEC_UTIL_HPP