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 | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_KEY_GEN_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_KEY_GEN_HPP |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 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'; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | std::string outputFilename; |
| 39 | |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 40 | po::options_description description("General Usage\n" |
| 41 | " ndnsec key-gen [-h] [-n] identity\n" |
| 42 | "General options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 43 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 44 | ("help,h", "produce help message") |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 45 | ("identity,i", po::value<std::string>(&identityName), |
| 46 | "identity name, for example, /ndn/edu/ucla/alice") |
| 47 | ("not_default,n", |
| 48 | "optional, if not specified, the target identity will be set as " |
| 49 | "the default identity of the system") |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 50 | ("dsk,d", "generate Data-Signing-Key (DSK) instead of the default Key-Signing-Key (KSK)") |
Yingdi Yu | 7d8644a | 2014-12-01 22:55:49 -0800 | [diff] [blame] | 51 | ("type,t", po::value<char>(&keyType)->default_value('r'), |
| 52 | "optional, key type, r for RSA key (default), e for ECDSA key") |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 53 | // ("size,s", po::value<int>(&keySize)->default_value(2048), |
| 54 | // "optional, key size, 2048 (default)") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 55 | ; |
| 56 | |
| 57 | po::positional_options_description p; |
| 58 | p.add("identity", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 59 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 60 | po::variables_map vm; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 61 | try { |
| 62 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 63 | vm); |
| 64 | po::notify(vm); |
| 65 | } |
| 66 | catch (const std::exception& e) { |
| 67 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 68 | std::cerr << description << std::endl; |
| 69 | return 1; |
| 70 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 72 | if (vm.count("help") != 0) { |
| 73 | std::cerr << description << std::endl; |
| 74 | return 0; |
| 75 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 77 | if (vm.count("identity") == 0) { |
| 78 | std::cerr << "identity must be specified" << std::endl; |
| 79 | std::cerr << description << std::endl; |
| 80 | return 1; |
| 81 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 82 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 83 | if (vm.count("not_default") != 0) |
| 84 | isDefault = false; |
| 85 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 86 | bool isKsk = (vm.count("dsk") == 0); |
| 87 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 88 | ndn::security::v1::KeyChain keyChain; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 89 | Name keyName; |
| 90 | |
| 91 | try { |
| 92 | switch (keyType) { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 93 | case 'r': |
Yingdi Yu | 7d8644a | 2014-12-01 22:55:49 -0800 | [diff] [blame] | 94 | keyName = keyChain.generateRsaKeyPair(Name(identityName), isKsk, RsaKeyParams().getKeySize()); |
| 95 | break; |
| 96 | case 'e': |
| 97 | keyName = keyChain.generateEcdsaKeyPair(Name(identityName), isKsk, |
| 98 | EcdsaKeyParams().getKeySize()); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 99 | break; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 100 | default: |
| 101 | std::cerr << "Unrecongized key type" << "\n"; |
| 102 | std::cerr << description << std::endl; |
| 103 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 106 | if (0 == keyName.size()) { |
| 107 | std::cerr << "Error: failed to generate key" << "\n"; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | keyChain.setDefaultKeyNameForIdentity(keyName); |
| 112 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 113 | shared_ptr<security::v1::IdentityCertificate> identityCert = keyChain.selfSign(keyName); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 114 | |
| 115 | if (isDefault) |
| 116 | keyChain.setDefaultIdentity(Name(identityName)); |
| 117 | |
| 118 | io::save(*identityCert, std::cout); |
| 119 | } |
| 120 | catch (const std::exception& e) { |
| 121 | std::cerr << "Error: " << e.what() << std::endl; |
| 122 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 126 | #endif // NDN_TOOLS_NDNSEC_KEY_GEN_HPP |