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