blob: 28c2a3119ad7223218cd35942eb75e6019a5751a [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_UTIL_HPP
16#define NDNSEC_UTIL_HPP
17
18#include <iostream>
19#include <fstream>
20#include <string>
21#include <cstring>
22
23#include <boost/program_options/options_description.hpp>
24#include <boost/program_options/variables_map.hpp>
25#include <boost/program_options/parsers.hpp>
26#include <boost/date_time/posix_time/posix_time.hpp>
27#include <boost/tokenizer.hpp>
28#include <boost/asio.hpp>
29#include <boost/exception/all.hpp>
30
31
Junxiao Shi482ccc52014-03-31 13:05:24 -070032#include "security/cryptopp.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033
34#include "security/key-chain.hpp"
Yingdi Yu64c3fb42014-02-26 17:30:04 -080035#include "util/io.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036
37bool
38getPassword(std::string& password, const std::string& prompt)
39{
Yingdi Yub61f5402014-02-26 17:46:11 -080040 bool isReady = false;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041
Yingdi Yub61f5402014-02-26 17:46:11 -080042 char* pw0 = 0;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070043
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 pw0 = getpass(prompt.c_str());
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070045 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 return false;
47 std::string password1 = pw0;
48 memset(pw0, 0, strlen(pw0));
49
50 pw0 = getpass("Confirm:");
Yingdi Yub61f5402014-02-26 17:46:11 -080051 if (!pw0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052 {
53 char* pw1 = const_cast<char*>(password1.c_str());
54 memset(pw1, 0, password1.size());
55 return false;
56 }
57
Yingdi Yub61f5402014-02-26 17:46:11 -080058 if (!password1.compare(pw0))
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059 {
Yingdi Yub61f5402014-02-26 17:46:11 -080060 isReady = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 password.swap(password1);
62 }
63
64 char* pw1 = const_cast<char*>(password1.c_str());
65 memset(pw1, 0, password1.size());
66 memset(pw0, 0, strlen(pw0));
67
Yingdi Yub61f5402014-02-26 17:46:11 -080068 if (password.empty())
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069 return false;
70
Yingdi Yub61f5402014-02-26 17:46:11 -080071 return isReady;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080072}
73
74ndn::shared_ptr<ndn::IdentityCertificate>
75getIdentityCertificate(const std::string& fileName)
76{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077
Yingdi Yub61f5402014-02-26 17:46:11 -080078 if (fileName == "-")
Yingdi Yu64c3fb42014-02-26 17:30:04 -080079 return ndn::io::load<ndn::IdentityCertificate>(std::cin);
80 else
81 return ndn::io::load<ndn::IdentityCertificate>(fileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082}
83
84#endif //NDNSEC_UTIL_HPP