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 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 13 | int |
| 14 | ndnsec_key_gen(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 15 | { |
| 16 | using namespace ndn; |
| 17 | namespace po = boost::program_options; |
| 18 | |
| 19 | std::string identityName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 20 | bool isDefault = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 21 | char keyType = 'r'; |
| 22 | int keySize = 2048; |
| 23 | std::string outputFilename; |
| 24 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 25 | po::options_description description("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options"); |
| 26 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 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); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 36 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 37 | po::variables_map vm; |
| 38 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 39 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 41 | vm); |
| 42 | po::notify(vm); |
| 43 | } |
| 44 | catch (const std::exception& e) |
| 45 | { |
| 46 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 47 | std::cerr << description << std::endl; |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | if (vm.count("help") != 0) |
| 52 | { |
| 53 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 57 | if (vm.count("identity") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 58 | { |
| 59 | std::cerr << "identity must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | return 1; |
| 62 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 63 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 64 | if (vm.count("not_default") != 0) |
| 65 | isDefault = false; |
| 66 | |
| 67 | switch (keyType) |
| 68 | { |
| 69 | case 'r': |
| 70 | { |
| 71 | shared_ptr<IdentityCertificate> identityCert; |
| 72 | |
| 73 | KeyChain keyChain; |
| 74 | |
| 75 | Name keyName = keyChain.generateRSAKeyPair(Name(identityName), true, keySize); |
| 76 | |
| 77 | if (0 == keyName.size()) |
| 78 | return 1; |
| 79 | |
| 80 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 81 | |
| 82 | identityCert = keyChain.selfSign(keyName); |
| 83 | |
| 84 | if (isDefault) |
| 85 | keyChain.setDefaultIdentity(Name(identityName)); |
| 86 | |
| 87 | io::save(*identityCert, std::cout); |
| 88 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 89 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 90 | default: |
| 91 | std::cerr << "Unrecongized key type" << "\n"; |
| 92 | std::cerr << description << std::endl; |
| 93 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | #endif //NDNSEC_KEY_GEN_HPP |