Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -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 | * BSD license, See the LICENSE file for more information |
| 5 | * Author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDNSEC_KEY_GEN_HPP |
| 9 | #define NDNSEC_KEY_GEN_HPP |
| 10 | |
| 11 | #include "ndnsec-util.hpp" |
| 12 | |
| 13 | int |
| 14 | ndnsec_key_gen(int argc, char** argv) |
| 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | std::string identityName; |
| 20 | bool notDefault = false; |
| 21 | char keyType = 'r'; |
| 22 | int keySize = 2048; |
| 23 | std::string outputFilename; |
| 24 | |
| 25 | po::options_description desc("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options"); |
| 26 | desc.add_options() |
| 27 | ("help,h", "produce help message") |
| 28 | ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice") |
| 29 | ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system") |
| 30 | // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)") |
| 31 | // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)") |
| 32 | ; |
| 33 | |
| 34 | po::positional_options_description p; |
| 35 | p.add("identity", 1); |
| 36 | |
| 37 | po::variables_map vm; |
| 38 | po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); |
| 39 | po::notify(vm); |
| 40 | |
| 41 | if (vm.count("help")) |
| 42 | { |
| 43 | std::cerr << desc << std::endl; |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | if (0 == vm.count("identity")) |
| 48 | { |
| 49 | std::cerr << "identity must be specified" << std::endl; |
| 50 | std::cerr << desc << std::endl; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | if (vm.count("not_default")) |
| 55 | notDefault = true; |
| 56 | |
| 57 | if (true) |
| 58 | { |
| 59 | switch(keyType) |
| 60 | { |
| 61 | case 'r': |
| 62 | { |
| 63 | shared_ptr<IdentityCertificate> idcert; |
| 64 | try |
| 65 | { |
| 66 | KeyChain keyChain; |
| 67 | |
| 68 | Name keyName = keyChain.generateRSAKeyPair(Name(identityName), true, keySize); |
| 69 | |
| 70 | if(0 == keyName.size()) |
| 71 | { |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 76 | |
| 77 | idcert = keyChain.selfSign(keyName); |
| 78 | |
| 79 | if(!notDefault) |
| 80 | { |
| 81 | keyChain.setDefaultIdentity(Name(identityName)); |
| 82 | } |
| 83 | } |
| 84 | catch(const SecPublicInfo::Error& e) |
| 85 | { |
| 86 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 87 | return 1; |
| 88 | } |
| 89 | catch(const SecTpm::Error& e) |
| 90 | { |
| 91 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | try |
| 96 | { |
| 97 | CryptoPP::StringSource ss(idcert->wireEncode().wire(), |
| 98 | idcert->wireEncode().size(), |
| 99 | true, |
| 100 | new CryptoPP::Base64Encoder(new CryptoPP::FileSink(std::cout), true, 64)); |
| 101 | return 0; |
| 102 | } |
| 103 | catch(const CryptoPP::Exception& e) |
| 104 | { |
| 105 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 106 | return 1; |
| 107 | } |
| 108 | } |
| 109 | default: |
| 110 | std::cerr << "Unrecongized key type" << "\n"; |
| 111 | std::cerr << desc << std::endl; |
| 112 | return 1; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | #endif //NDNSEC_KEY_GEN_HPP |