blob: 77c7d57f0b678cc902682af3bdf9f35765f5db18 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang457f09a2018-01-05 13:45:38 -08002/*
Davide Pesavento8e047e12024-02-12 16:50:23 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040025namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080026
Yingdi Yub61f5402014-02-26 17:46:11 -080027int
28ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080029{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030 namespace po = boost::program_options;
31
Alexander Afanasyev35109a12017-01-04 15:39:06 -080032 Name identityName;
Davide Pesaventob310efb2019-04-11 22:10:24 -040033 bool wantNotDefault = false;
34 char keyTypeChoice;
35 char keyIdTypeChoice;
36 std::string userKeyId;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 po::options_description description(
Davide Pesavento25d9c9f2024-06-23 15:10:05 -040039 "Usage: ndnsec key-gen [-h] [-n] [-t TYPE] [-k KEYIDTYPE|--keyid KEYID]\n"
40 " [-i] IDENTITY\n"
41 "\n"
42 "Generate a public/private key pair for an identity.\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 "\n"
44 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080045 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040047 ("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 Pesavento702467b2020-04-22 21:41:23 -040049 ("type,t", po::value<char>(&keyTypeChoice)->default_value('e'),
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070050 "key type: 'r' for RSA, 'e' for ECDSA")
51 ("keyid-type,k", po::value<char>(&keyIdTypeChoice),
Davide Pesavento58de07a2022-06-08 00:02:02 -040052 "key ID type: 'h' for the SHA-256 of the public key, 'r' for a 64-bit "
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070053 "random number (the default unless --keyid is specified)")
Davide Pesavento58de07a2022-06-08 00:02:02 -040054 ("keyid", po::value<std::string>(&userKeyId),
55 "user-specified key ID, interpreted as a name component in URI format")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080056 ;
57
58 po::positional_options_description p;
59 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080060
Yingdi Yub61f5402014-02-26 17:46:11 -080061 po::variables_map vm;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080062 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080064 po::notify(vm);
65 }
66 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 std::cerr << "ERROR: " << e.what() << "\n\n"
68 << description << std::endl;
69 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080070 }
Yingdi Yub61f5402014-02-26 17:46:11 -080071
Davide Pesaventob310efb2019-04-11 22:10:24 -040072 if (vm.count("help") > 0) {
73 std::cout << description << std::endl;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080074 return 0;
75 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080077 if (vm.count("identity") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040078 std::cerr << "ERROR: you must specify an identity" << std::endl;
79 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080080 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081
Davide Pesaventob310efb2019-04-11 22:10:24 -040082 KeyIdType keyIdType = KeyIdType::RANDOM;
83 Name::Component userKeyIdComponent;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080084
Davide Pesaventob310efb2019-04-11 22:10:24 -040085 if (vm.count("keyid") > 0) {
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070086 if (vm.count("keyid-type") > 0) {
87 std::cerr << "ERROR: cannot specify both '--keyid' and '--keyid-type'" << std::endl;
88 return 2;
89 }
90
Davide Pesaventob310efb2019-04-11 22:10:24 -040091 keyIdType = KeyIdType::USER_SPECIFIED;
Davide Pesavento8e047e12024-02-12 16:50:23 -050092 userKeyIdComponent = name::Component::fromUri(userKeyId);
Davide Pesavento58de07a2022-06-08 00:02:02 -040093 if (userKeyIdComponent.empty() ||
94 userKeyIdComponent.isImplicitSha256Digest() ||
95 userKeyIdComponent.isParametersSha256Digest() ||
96 userKeyIdComponent.isKeyword()) {
97 std::cerr << "ERROR: '" << userKeyIdComponent << "' cannot be used as key ID" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040098 return 2;
99 }
100 }
101
102 if (vm.count("keyid-type") > 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400103 switch (keyIdTypeChoice) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400104 case 'h':
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800105 keyIdType = KeyIdType::SHA256;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400106 break;
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -0700107 case 'r':
108 // KeyIdType::RANDOM is the default
109 break;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400110 default:
Davide Pesavento58de07a2022-06-08 00:02:02 -0400111 std::cerr << "ERROR: unrecognized key ID type '" << keyIdTypeChoice << "'" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400112 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 Zhang457f09a2018-01-05 13:45:38 -0800121 }
122 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400123 params = make_unique<RsaKeyParams>(detail::RsaKeyParamsInfo::getDefaultSize(), keyIdType);
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800124 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400125 break;
126 case 'e':
127 if (keyIdType == KeyIdType::USER_SPECIFIED) {
128 params = make_unique<EcKeyParams>(userKeyIdComponent);
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800129 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400130 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 Zhang457f09a2018-01-05 13:45:38 -0800137 }
138
Davide Pesaventof2cae612021-03-24 18:47:05 -0400139 KeyChain keyChain;
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800140
Davide Pesaventob310efb2019-04-11 22:10:24 -0400141 security::Identity identity;
142 security::Key key;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800143 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400144 identity = keyChain.getPib().getIdentity(identityName);
145 key = keyChain.createKey(identity, *params);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800146 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400147 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 Afanasyev2a047eb2014-11-30 22:45:02 -0800151 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400152
153 if (!wantNotDefault) {
154 keyChain.setDefaultKey(identity, key);
155 keyChain.setDefaultIdentity(identity);
156 }
157
158 io::save(key.getDefaultCertificate(), std::cout);
159
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800160 return 0;
161}
162
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400163} // namespace ndn::ndnsec