Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 2 | /** |
| 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 Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef NDNSEC_KEY_GEN_HPP |
| 16 | #define NDNSEC_KEY_GEN_HPP |
| 17 | |
| 18 | #include "ndnsec-util.hpp" |
| 19 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 20 | int |
| 21 | ndnsec_key_gen(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | { |
| 23 | using namespace ndn; |
| 24 | namespace po = boost::program_options; |
| 25 | |
| 26 | std::string identityName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 27 | bool isDefault = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 28 | char keyType = 'r'; |
| 29 | int keySize = 2048; |
| 30 | std::string outputFilename; |
| 31 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 32 | po::options_description description("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options"); |
| 33 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 34 | ("help,h", "produce help message") |
| 35 | ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice") |
| 36 | ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system") |
| 37 | // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)") |
| 38 | // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)") |
| 39 | ; |
| 40 | |
| 41 | po::positional_options_description p; |
| 42 | p.add("identity", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 44 | po::variables_map vm; |
| 45 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 46 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 47 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 48 | vm); |
| 49 | po::notify(vm); |
| 50 | } |
| 51 | catch (const std::exception& e) |
| 52 | { |
| 53 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 54 | std::cerr << description << std::endl; |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | if (vm.count("help") != 0) |
| 59 | { |
| 60 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 64 | if (vm.count("identity") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 65 | { |
| 66 | std::cerr << "identity must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 67 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 68 | return 1; |
| 69 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | if (vm.count("not_default") != 0) |
| 72 | isDefault = false; |
| 73 | |
| 74 | switch (keyType) |
| 75 | { |
| 76 | case 'r': |
| 77 | { |
| 78 | shared_ptr<IdentityCertificate> identityCert; |
| 79 | |
| 80 | KeyChain keyChain; |
| 81 | |
| 82 | Name keyName = keyChain.generateRSAKeyPair(Name(identityName), true, keySize); |
| 83 | |
| 84 | if (0 == keyName.size()) |
| 85 | return 1; |
| 86 | |
| 87 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 88 | |
| 89 | identityCert = keyChain.selfSign(keyName); |
| 90 | |
| 91 | if (isDefault) |
| 92 | keyChain.setDefaultIdentity(Name(identityName)); |
| 93 | |
| 94 | io::save(*identityCert, std::cout); |
| 95 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 96 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 97 | default: |
| 98 | std::cerr << "Unrecongized key type" << "\n"; |
| 99 | std::cerr << description << std::endl; |
| 100 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | #endif //NDNSEC_KEY_GEN_HPP |