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 <cryptopp/base64.h> |
| 18 | #include <cryptopp/files.h> |
| 19 | |
| 20 | #include "security/key-chain.hpp" |
| 21 | |
| 22 | using namespace ndn; |
| 23 | namespace po = boost::program_options; |
| 24 | |
| 25 | |
| 26 | int main(int argc, char** argv) |
| 27 | { |
| 28 | std::string name; |
| 29 | bool isKeyName = false; |
| 30 | |
| 31 | po::options_description desc("General Usage\n ndn-sign-req [-h] [-k] name\nGeneral options"); |
| 32 | desc.add_options() |
| 33 | ("help,h", "produce help message") |
| 34 | ("key,k", "optional, if specified, name is keyName (e.g. /ndn/edu/ucla/alice/ksk-123456789), otherwise identity name") |
| 35 | ("name,n", po::value<std::string>(&name), "name, for example, /ndn/edu/ucla/alice") |
| 36 | ; |
| 37 | |
| 38 | po::positional_options_description p; |
| 39 | p.add("name", 1); |
| 40 | |
| 41 | po::variables_map vm; |
| 42 | try { |
| 43 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 44 | po::notify(vm); |
| 45 | } |
| 46 | catch(const std::exception &e) { |
| 47 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 48 | std::cerr << desc << std::endl; |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | if (vm.count("help")) |
| 53 | { |
| 54 | std::cerr << desc << std::endl; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | if (0 == vm.count("name")) |
| 59 | { |
| 60 | std::cerr << "identity_name must be specified" << std::endl; |
| 61 | std::cerr << desc << std::endl; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | if (vm.count("key")) |
| 66 | isKeyName = true; |
| 67 | |
| 68 | KeyChain keyChain; |
| 69 | |
| 70 | try{ |
| 71 | if(isKeyName) |
| 72 | { |
| 73 | shared_ptr<IdentityCertificate> selfSignCert = keyChain.selfSign(name); |
| 74 | |
| 75 | CryptoPP::StringSource ss(selfSignCert->wireEncode().wire(), selfSignCert->wireEncode().size(), true, |
| 76 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(std::cout), true, 64)); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | Name keyName = keyChain.getDefaultKeyNameForIdentity(name); |
| 81 | ptr_lib::shared_ptr<IdentityCertificate> selfSignCert = keyChain.selfSign(keyName); |
| 82 | |
| 83 | CryptoPP::StringSource ss(selfSignCert->wireEncode().wire(), selfSignCert->wireEncode().size(), true, |
| 84 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(std::cout), true, 64)); |
| 85 | } |
| 86 | } |
| 87 | catch(std::exception & e) |
| 88 | { |
| 89 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 90 | return 1; |
| 91 | } |
| 92 | return 0; |
| 93 | } |