Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 31 | namespace po = boost::program_options; |
| 32 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 33 | Name identityName; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 34 | bool wantNotDefault = false; |
| 35 | char keyTypeChoice; |
| 36 | char keyIdTypeChoice; |
| 37 | std::string userKeyId; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 39 | po::options_description description( |
| 40 | "Usage: ndnsec key-gen [-h] [-n] [-t TYPE] [-k IDTYPE] [-i] IDENTITY\n" |
| 41 | "\n" |
| 42 | "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") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 45 | ("identity,i", po::value<Name>(&identityName), "identity name, e.g., /ndn/edu/ucla/alice") |
| 46 | ("not-default,n", po::bool_switch(&wantNotDefault), "do not set the identity as default") |
| 47 | ("type,t", po::value<char>(&keyTypeChoice)->default_value('r'), |
| 48 | "key type, 'r' for RSA, 'e' for ECDSA") |
| 49 | ("keyid-type,k", po::value<char>(&keyIdTypeChoice)->default_value('r'), |
| 50 | "key id type, 'r' for 64-bit random number, 'h' for SHA256 of the public key") |
| 51 | ("keyid", po::value<std::string>(&userKeyId), "user-specified key id") |
| 52 | //("size,s", po::value<int>(&keySize)->default_value(2048), "key size in bits") |
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) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 64 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 65 | << description << std::endl; |
| 66 | return 2; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 67 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 68 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 69 | if (vm.count("help") > 0) { |
| 70 | std::cout << description << std::endl; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 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) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 75 | std::cerr << "ERROR: you must specify an identity" << std::endl; |
| 76 | return 2; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 77 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 78 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 79 | KeyIdType keyIdType = KeyIdType::RANDOM; |
| 80 | Name::Component userKeyIdComponent; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 81 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 82 | if (vm.count("keyid") > 0) { |
| 83 | keyIdType = KeyIdType::USER_SPECIFIED; |
| 84 | userKeyIdComponent = name::Component::fromEscapedString(userKeyId); |
| 85 | if (userKeyIdComponent.empty()) { |
| 86 | std::cerr << "ERROR: key id cannot be an empty name component" << std::endl; |
| 87 | return 2; |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 88 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 89 | if (!userKeyIdComponent.isGeneric()) { |
| 90 | std::cerr << "ERROR: key id must be a GenericNameComponent" << std::endl; |
| 91 | return 2; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (vm.count("keyid-type") > 0) { |
| 96 | if (keyIdType == KeyIdType::USER_SPECIFIED) { |
| 97 | std::cerr << "ERROR: cannot specify both '--keyid' and '--keyid-type'" << std::endl; |
| 98 | return 2; |
| 99 | } |
| 100 | |
| 101 | switch (keyIdTypeChoice) { |
| 102 | case 'r': |
| 103 | // KeyIdType::RANDOM is the default |
| 104 | break; |
| 105 | case 'h': |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 106 | keyIdType = KeyIdType::SHA256; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | std::cerr << "ERROR: unrecognized key id type '" << keyIdTypeChoice << "'" << std::endl; |
| 110 | return 2; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | unique_ptr<KeyParams> params; |
| 115 | switch (keyTypeChoice) { |
| 116 | case 'r': |
| 117 | if (keyIdType == KeyIdType::USER_SPECIFIED) { |
| 118 | params = make_unique<RsaKeyParams>(userKeyIdComponent); |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 119 | } |
| 120 | else { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 121 | params = make_unique<RsaKeyParams>(detail::RsaKeyParamsInfo::getDefaultSize(), keyIdType); |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 122 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 123 | break; |
| 124 | case 'e': |
| 125 | if (keyIdType == KeyIdType::USER_SPECIFIED) { |
| 126 | params = make_unique<EcKeyParams>(userKeyIdComponent); |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 127 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 128 | else { |
| 129 | params = make_unique<EcKeyParams>(detail::EcKeyParamsInfo::getDefaultSize(), keyIdType); |
| 130 | } |
| 131 | break; |
| 132 | default: |
| 133 | std::cerr << "ERROR: unrecognized key type '" << keyTypeChoice << "'" << std::endl; |
| 134 | return 2; |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 137 | security::v2::KeyChain keyChain; |
Zhiyi Zhang | 457f09a | 2018-01-05 13:45:38 -0800 | [diff] [blame] | 138 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 139 | security::Identity identity; |
| 140 | security::Key key; |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 141 | try { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 142 | identity = keyChain.getPib().getIdentity(identityName); |
| 143 | key = keyChain.createKey(identity, *params); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 144 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 145 | catch (const security::Pib::Error&) { |
| 146 | // identity doesn't exist, so create it and generate key |
| 147 | identity = keyChain.createIdentity(identityName, *params); |
| 148 | key = identity.getDefaultKey(); |
Alexander Afanasyev | 2a047eb | 2014-11-30 22:45:02 -0800 | [diff] [blame] | 149 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 150 | |
| 151 | if (!wantNotDefault) { |
| 152 | keyChain.setDefaultKey(identity, key); |
| 153 | keyChain.setDefaultIdentity(identity); |
| 154 | } |
| 155 | |
| 156 | io::save(key.getDefaultCertificate(), std::cout); |
| 157 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 161 | } // namespace ndnsec |
| 162 | } // namespace ndn |