blob: ac85fe8bc6ade68a6f5da8f4ab4164b4556bc9ad [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 Pesavento58de07a2022-06-08 00:02:02 -04003 * Copyright (c) 2013-2022 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
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yub61f5402014-02-26 17:46:11 -080028int
29ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 namespace po = boost::program_options;
32
Alexander Afanasyev35109a12017-01-04 15:39:06 -080033 Name identityName;
Davide Pesaventob310efb2019-04-11 22:10:24 -040034 bool wantNotDefault = false;
35 char keyTypeChoice;
36 char keyIdTypeChoice;
37 std::string userKeyId;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038
Davide Pesaventob310efb2019-04-11 22:10:24 -040039 po::options_description description(
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070040 "Usage: ndnsec key-gen [-h] [-n] [-t TYPE] [-k KEYIDTYPE|--keyid KEYID] [-i] IDENTITY\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040041 "\n"
42 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080043 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080044 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040045 ("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")
Davide Pesavento702467b2020-04-22 21:41:23 -040047 ("type,t", po::value<char>(&keyTypeChoice)->default_value('e'),
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070048 "key type: 'r' for RSA, 'e' for ECDSA")
49 ("keyid-type,k", po::value<char>(&keyIdTypeChoice),
Davide Pesavento58de07a2022-06-08 00:02:02 -040050 "key ID type: 'h' for the SHA-256 of the public key, 'r' for a 64-bit "
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070051 "random number (the default unless --keyid is specified)")
Davide Pesavento58de07a2022-06-08 00:02:02 -040052 ("keyid", po::value<std::string>(&userKeyId),
53 "user-specified key ID, interpreted as a name component in URI format")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 ;
55
56 po::positional_options_description p;
57 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080058
Yingdi Yub61f5402014-02-26 17:46:11 -080059 po::variables_map vm;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080060 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080061 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080062 po::notify(vm);
63 }
64 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040065 std::cerr << "ERROR: " << e.what() << "\n\n"
66 << description << std::endl;
67 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080068 }
Yingdi Yub61f5402014-02-26 17:46:11 -080069
Davide Pesaventob310efb2019-04-11 22:10:24 -040070 if (vm.count("help") > 0) {
71 std::cout << description << std::endl;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080072 return 0;
73 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080075 if (vm.count("identity") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040076 std::cerr << "ERROR: you must specify an identity" << std::endl;
77 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080078 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079
Davide Pesaventob310efb2019-04-11 22:10:24 -040080 KeyIdType keyIdType = KeyIdType::RANDOM;
81 Name::Component userKeyIdComponent;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080082
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 if (vm.count("keyid") > 0) {
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070084 if (vm.count("keyid-type") > 0) {
85 std::cerr << "ERROR: cannot specify both '--keyid' and '--keyid-type'" << std::endl;
86 return 2;
87 }
88
Davide Pesaventob310efb2019-04-11 22:10:24 -040089 keyIdType = KeyIdType::USER_SPECIFIED;
90 userKeyIdComponent = name::Component::fromEscapedString(userKeyId);
Davide Pesavento58de07a2022-06-08 00:02:02 -040091 if (userKeyIdComponent.empty() ||
92 userKeyIdComponent.isImplicitSha256Digest() ||
93 userKeyIdComponent.isParametersSha256Digest() ||
94 userKeyIdComponent.isKeyword()) {
95 std::cerr << "ERROR: '" << userKeyIdComponent << "' cannot be used as key ID" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040096 return 2;
97 }
98 }
99
100 if (vm.count("keyid-type") > 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400101 switch (keyIdTypeChoice) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400102 case 'h':
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800103 keyIdType = KeyIdType::SHA256;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400104 break;
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -0700105 case 'r':
106 // KeyIdType::RANDOM is the default
107 break;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400108 default:
Davide Pesavento58de07a2022-06-08 00:02:02 -0400109 std::cerr << "ERROR: unrecognized key ID type '" << keyIdTypeChoice << "'" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400110 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 Zhang457f09a2018-01-05 13:45:38 -0800119 }
120 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400121 params = make_unique<RsaKeyParams>(detail::RsaKeyParamsInfo::getDefaultSize(), keyIdType);
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800122 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400123 break;
124 case 'e':
125 if (keyIdType == KeyIdType::USER_SPECIFIED) {
126 params = make_unique<EcKeyParams>(userKeyIdComponent);
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800127 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400128 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 Zhang457f09a2018-01-05 13:45:38 -0800135 }
136
Davide Pesaventof2cae612021-03-24 18:47:05 -0400137 KeyChain keyChain;
Zhiyi Zhang457f09a2018-01-05 13:45:38 -0800138
Davide Pesaventob310efb2019-04-11 22:10:24 -0400139 security::Identity identity;
140 security::Key key;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800141 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400142 identity = keyChain.getPib().getIdentity(identityName);
143 key = keyChain.createKey(identity, *params);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800144 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400145 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 Afanasyev2a047eb2014-11-30 22:45:02 -0800149 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400150
151 if (!wantNotDefault) {
152 keyChain.setDefaultKey(identity, key);
153 keyChain.setDefaultIdentity(identity);
154 }
155
156 io::save(key.getDefaultCertificate(), std::cout);
157
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800158 return 0;
159}
160
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800161} // namespace ndnsec
162} // namespace ndn