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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 28 | int |
| 29 | ndnsec_key_gen(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | { |
| 31 | using namespace ndn; |
| 32 | namespace po = boost::program_options; |
| 33 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 34 | Name identityName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 35 | bool isDefault = true; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 36 | char keyType = 'r'; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 37 | std::string outputFilename; |
| 38 | |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 39 | po::options_description description("General Usage\n" |
| 40 | " ndnsec key-gen [-h] [-n] identity\n" |
| 41 | "General options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 42 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | ("help,h", "produce help message") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 44 | ("identity,i", po::value<Name>(&identityName), |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 45 | "identity name, for example, /ndn/edu/ucla/alice") |
| 46 | ("not_default,n", |
| 47 | "optional, if not specified, the target identity will be set as " |
| 48 | "the default identity of the system") |
Yingdi Yu | 7d8644a | 2014-12-01 22:55:49 -0800 | [diff] [blame] | 49 | ("type,t", po::value<char>(&keyType)->default_value('r'), |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 50 | "optional, key type, r for RSA key (default), e for EC key") |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 51 | // ("size,s", po::value<int>(&keySize)->default_value(2048), |
| 52 | // "optional, key size, 2048 (default)") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 53 | ; |
| 54 | |
| 55 | po::positional_options_description p; |
| 56 | p.add("identity", 1); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 57 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | po::variables_map vm; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 59 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 60 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 61 | po::notify(vm); |
| 62 | } |
| 63 | catch (const std::exception& e) { |
| 64 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 65 | std::cerr << description << std::endl; |
| 66 | return 1; |
| 67 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 69 | if (vm.count("help") != 0) { |
| 70 | std::cerr << description << std::endl; |
| 71 | return 0; |
| 72 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 74 | if (vm.count("identity") == 0) { |
| 75 | std::cerr << "identity must be specified" << std::endl; |
| 76 | std::cerr << description << std::endl; |
| 77 | return 1; |
| 78 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 80 | if (vm.count("not_default") != 0) { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 81 | isDefault = false; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 83 | |
| 84 | try { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 85 | unique_ptr<KeyParams> params; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 86 | switch (keyType) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 87 | case 'r': |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 88 | params = make_unique<RsaKeyParams>(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 89 | break; |
| 90 | case 'e': |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 91 | params = make_unique<EcKeyParams>(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 92 | break; |
| 93 | default: |
| 94 | std::cerr << "Unrecongized key type\n" |
| 95 | << description << std::endl; |
| 96 | return 1; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 99 | // @TODO set other parameters based on whatever user specified |
| 100 | |
| 101 | security::v2::KeyChain keyChain; |
| 102 | security::Identity identity; |
| 103 | security::Key key; |
| 104 | try { |
| 105 | identity = keyChain.getPib().getIdentity(identityName); |
| 106 | key = keyChain.createKey(identity, *params); |
| 107 | } |
| 108 | catch (const security::Pib::Error&) { |
| 109 | // identity doesn't exist, so create it and generate key |
| 110 | identity = keyChain.createIdentity(identityName, *params); |
| 111 | key = identity.getDefaultKey(); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 114 | if (isDefault) { |
| 115 | keyChain.setDefaultKey(identity, key); |
| 116 | keyChain.setDefaultIdentity(identity); |
| 117 | } |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 119 | io::save(key.getDefaultCertificate(), std::cout); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 120 | } |
| 121 | catch (const std::exception& e) { |
| 122 | std::cerr << "Error: " << e.what() << std::endl; |
| 123 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 127 | } // namespace ndnsec |
| 128 | } // namespace ndn |