Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDNSEC_KEY_GEN_HPP |
| 25 | #define NDNSEC_KEY_GEN_HPP |
| 26 | |
| 27 | #include "ndnsec-util.hpp" |
| 28 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | int |
| 30 | ndnsec_key_gen(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | { |
| 32 | using namespace ndn; |
| 33 | namespace po = boost::program_options; |
| 34 | |
| 35 | std::string identityName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 36 | bool isDefault = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | char keyType = 'r'; |
| 38 | int keySize = 2048; |
| 39 | std::string outputFilename; |
| 40 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 41 | po::options_description description("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options"); |
| 42 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | ("help,h", "produce help message") |
| 44 | ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice") |
| 45 | ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system") |
| 46 | // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)") |
| 47 | // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)") |
| 48 | ; |
| 49 | |
| 50 | po::positional_options_description p; |
| 51 | p.add("identity", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 52 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 53 | po::variables_map vm; |
| 54 | try |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 56 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 57 | vm); |
| 58 | po::notify(vm); |
| 59 | } |
| 60 | catch (const std::exception& e) |
| 61 | { |
| 62 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 63 | std::cerr << description << std::endl; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | if (vm.count("help") != 0) |
| 68 | { |
| 69 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 73 | if (vm.count("identity") == 0) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 74 | { |
| 75 | std::cerr << "identity must be specified" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 77 | return 1; |
| 78 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 79 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | if (vm.count("not_default") != 0) |
| 81 | isDefault = false; |
| 82 | |
| 83 | switch (keyType) |
| 84 | { |
| 85 | case 'r': |
| 86 | { |
| 87 | shared_ptr<IdentityCertificate> identityCert; |
| 88 | |
| 89 | KeyChain keyChain; |
| 90 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 91 | Name keyName = keyChain.generateRsaKeyPair(Name(identityName), true, keySize); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 92 | |
| 93 | if (0 == keyName.size()) |
| 94 | return 1; |
| 95 | |
| 96 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 97 | |
| 98 | identityCert = keyChain.selfSign(keyName); |
| 99 | |
| 100 | if (isDefault) |
| 101 | keyChain.setDefaultIdentity(Name(identityName)); |
| 102 | |
| 103 | io::save(*identityCert, std::cout); |
| 104 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 105 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 106 | default: |
| 107 | std::cerr << "Unrecongized key type" << "\n"; |
| 108 | std::cerr << description << std::endl; |
| 109 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | #endif //NDNSEC_KEY_GEN_HPP |