Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -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 | * Yingdi Yu |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <iostream> |
| 12 | #include <fstream> |
| 13 | |
| 14 | #include <boost/program_options/options_description.hpp> |
| 15 | #include <boost/program_options/variables_map.hpp> |
| 16 | #include <boost/program_options/parsers.hpp> |
| 17 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 18 | #include <boost/asio.hpp> |
| 19 | |
| 20 | #include <cryptopp/base64.h> |
| 21 | #include <cryptopp/files.h> |
| 22 | |
| 23 | #include "security/key-chain.hpp" |
| 24 | |
| 25 | using namespace ndn; |
| 26 | namespace po = boost::program_options; |
| 27 | |
| 28 | shared_ptr<IdentityCertificate> |
| 29 | getIdentityCertificate(const std::string& fileName) |
| 30 | { |
| 31 | std::istream* ifs; |
| 32 | if(fileName == "-") |
| 33 | ifs = &std::cin; |
| 34 | else |
| 35 | ifs = new std::ifstream(fileName.c_str()); |
| 36 | |
| 37 | std::string decoded; |
| 38 | CryptoPP::FileSource ss2(*ifs, true, |
| 39 | new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded))); |
| 40 | |
| 41 | ptr_lib::shared_ptr<IdentityCertificate> identityCertificate = ptr_lib::make_shared<IdentityCertificate>(); |
| 42 | identityCertificate->wireDecode(Block(decoded.c_str(), decoded.size())); |
| 43 | |
| 44 | return identityCertificate; |
| 45 | } |
| 46 | |
| 47 | int main(int argc, char** argv) |
| 48 | { |
| 49 | std::string name; |
| 50 | bool isKeyName = false; |
| 51 | bool isIdentityName = false; |
| 52 | bool isCertName = true; |
| 53 | bool isFileName = false; |
| 54 | bool isPretty = false; |
| 55 | bool isStdOut = true; |
| 56 | bool isRepoOut = false; |
| 57 | std::string repoHost = "127.0.0.1"; |
| 58 | std::string repoPort = "7376"; |
| 59 | bool isDnsOut = false; |
| 60 | |
| 61 | po::options_description desc("General Usage\n ndn-dump-certificate [-h] [-p] [-d] [-r [-H repo-host] [-P repor-port] ] [-i|k|f] certName\nGeneral options"); |
| 62 | desc.add_options() |
| 63 | ("help,h", "produce help message") |
| 64 | ("pretty,p", "optional, if specified, display certificate in human readable format") |
| 65 | ("identity,i", "optional, if specified, name is identity name (e.g. /ndn/edu/ucla/alice), otherwise certificate name") |
| 66 | ("key,k", "optional, if specified, name is key name (e.g. /ndn/edu/ucla/alice/KSK-123456789), otherwise certificate name") |
| 67 | ("file,f", "optional, if specified, name is file name, - for stdin") |
| 68 | ("repo-output,r", "optional, if specified, certificate is dumped (published) to repo") |
| 69 | ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), "optional, the repo host if repo-output is specified") |
| 70 | ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), "optional, the repo port if repo-output is specified") |
| 71 | ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS") |
| 72 | ("name,n", po::value<std::string>(&name), "certificate name, for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF") |
| 73 | ; |
| 74 | |
| 75 | po::positional_options_description p; |
| 76 | p.add("name", 1); |
| 77 | |
| 78 | po::variables_map vm; |
| 79 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 80 | po::notify(vm); |
| 81 | |
| 82 | if (vm.count("help")) |
| 83 | { |
| 84 | std::cerr << desc << std::endl; |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | if (0 == vm.count("name")) |
| 89 | { |
| 90 | std::cerr << "identity_name must be specified" << std::endl; |
| 91 | std::cerr << desc << std::endl; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | if (vm.count("key")) |
| 96 | { |
| 97 | isCertName = false; |
| 98 | isKeyName = true; |
| 99 | } |
| 100 | else if (vm.count("identity")) |
| 101 | { |
| 102 | isCertName = false; |
| 103 | isIdentityName = true; |
| 104 | } |
| 105 | else if (vm.count("file")) |
| 106 | { |
| 107 | isCertName = false; |
| 108 | isFileName = true; |
| 109 | } |
| 110 | |
| 111 | if (vm.count("pretty")) |
| 112 | isPretty = true; |
| 113 | |
| 114 | if (vm.count("repo-output")) |
| 115 | { |
| 116 | isRepoOut = true; |
| 117 | isStdOut = false; |
| 118 | } |
| 119 | else if(vm.count("dns-output")) |
| 120 | { |
| 121 | isDnsOut = true; |
| 122 | isStdOut = false; |
| 123 | std::cerr << "Error: DNS output is not supported yet!" << std::endl; |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | if (isPretty && !isStdOut) |
| 128 | { |
| 129 | std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl; |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | KeyChain keyChain; |
| 134 | ptr_lib::shared_ptr<IdentityCertificate> certificate; |
| 135 | |
| 136 | try{ |
| 137 | if(isIdentityName || isKeyName || isCertName) |
| 138 | { |
| 139 | if(isIdentityName) |
| 140 | { |
| 141 | Name certName = keyChain.getDefaultCertificateNameForIdentity(name); |
| 142 | certificate = keyChain.getCertificate(certName); |
| 143 | } |
| 144 | else if(isKeyName) |
| 145 | { |
| 146 | Name certName = keyChain.getDefaultCertificateNameForKey(name); |
| 147 | certificate = keyChain.getCertificate(certName); |
| 148 | } |
| 149 | else |
| 150 | certificate = keyChain.getCertificate(name); |
| 151 | |
| 152 | if(NULL == certificate) |
| 153 | { |
| 154 | std::cerr << "No certificate found!" << std::endl; |
| 155 | return 1; |
| 156 | } |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | certificate = getIdentityCertificate(name); |
| 161 | if(NULL == certificate) |
| 162 | { |
| 163 | std::cerr << "No certificate read!" << std::endl; |
| 164 | return 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if(isPretty) |
| 169 | { |
| 170 | std::cout << *certificate << std::endl; |
| 171 | // cout << "Certificate name: " << std::endl; |
| 172 | // cout << " " << certificate->getName() << std::endl; |
| 173 | // cout << "Validity: " << std::endl; |
| 174 | // cout << " NotBefore: " << boost::posix_time::to_simple_string(certificate->getNotBefore()) << std::endl; |
| 175 | // cout << " NotAfter: " << boost::posix_time::to_simple_string(certificate->getNotAfter()) << std::endl; |
| 176 | // cout << "Subject Description: " << std::endl; |
| 177 | // const vector<CertificateSubjectDescription>& SubDescriptionList = certificate->getSubjectDescriptionList(); |
| 178 | // vector<CertificateSubjectDescription>::const_iterator it = SubDescriptionList.begin(); |
| 179 | // for(; it != SubDescriptionList.end(); it++) |
| 180 | // cout << " " << it->getOidStr() << ": " << it->getValue() << std::endl; |
| 181 | // cout << "Public key bits: " << std::endl; |
| 182 | // const Blob& keyBlob = certificate->getPublicKeygetKeyBlob(); |
| 183 | // std::string encoded; |
| 184 | // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(keyBlob.buf()), keyBlob.size(), true, |
| 185 | // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), true, 64)); |
| 186 | // cout << encoded; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | if(isStdOut) |
| 191 | { |
| 192 | CryptoPP::StringSource ss(certificate->wireEncode().wire(), certificate->wireEncode().size(), |
| 193 | true, |
| 194 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(std::cout), true, 64)); |
| 195 | return 0; |
| 196 | } |
| 197 | if(isRepoOut) |
| 198 | { |
| 199 | using namespace boost::asio::ip; |
| 200 | tcp::iostream request_stream; |
| 201 | #if (BOOST_VERSION >= 104700) |
| 202 | request_stream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 203 | #endif |
| 204 | request_stream.connect(repoHost,repoPort); |
| 205 | if(!request_stream) |
| 206 | { |
| 207 | std::cerr << "fail to open the stream!" << std::endl; |
| 208 | return 1; |
| 209 | } |
| 210 | request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()), certificate->wireEncode().size()); |
| 211 | return 0; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | catch(std::exception & e){ |
| 216 | std::cerr << e.what() << std::endl; |
| 217 | return 1; |
| 218 | } |
| 219 | return 0; |
| 220 | } |