blob: 2090f74d9c3d3a837e4b560d3bc4a41dfa788e7e [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 Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 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 Pesavento25d4f1c2020-04-29 23:31:04 -040025#include "ndn-cxx/util/io.hpp"
26
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080027namespace ndn {
28namespace ndnsec {
29
Yingdi Yub61f5402014-02-26 17:46:11 -080030int
31ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033 namespace po = boost::program_options;
34
Alexander Afanasyev35109a12017-01-04 15:39:06 -080035 Name identityName;
Davide Pesaventob310efb2019-04-11 22:10:24 -040036 bool wantNotDefault = false;
37 char keyTypeChoice;
38 char keyIdTypeChoice;
39 std::string userKeyId;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040
Davide Pesaventob310efb2019-04-11 22:10:24 -040041 po::options_description description(
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070042 "Usage: ndnsec key-gen [-h] [-n] [-t TYPE] [-k KEYIDTYPE|--keyid KEYID] [-i] 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),
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 Pesaventob310efb2019-04-11 22:10:24 -040054 ("keyid", po::value<std::string>(&userKeyId), "user-specified key id")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080055 ;
56
57 po::positional_options_description p;
58 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080059
Yingdi Yub61f5402014-02-26 17:46:11 -080060 po::variables_map vm;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080061 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080062 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080063 po::notify(vm);
64 }
65 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040066 std::cerr << "ERROR: " << e.what() << "\n\n"
67 << description << std::endl;
68 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080069 }
Yingdi Yub61f5402014-02-26 17:46:11 -080070
Davide Pesaventob310efb2019-04-11 22:10:24 -040071 if (vm.count("help") > 0) {
72 std::cout << description << std::endl;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080073 return 0;
74 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080076 if (vm.count("identity") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040077 std::cerr << "ERROR: you must specify an identity" << std::endl;
78 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080079 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080080
Davide Pesaventob310efb2019-04-11 22:10:24 -040081 KeyIdType keyIdType = KeyIdType::RANDOM;
82 Name::Component userKeyIdComponent;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080083
Davide Pesaventob310efb2019-04-11 22:10:24 -040084 if (vm.count("keyid") > 0) {
Zhiyi Zhang4c68b6e2020-04-20 16:13:21 -070085 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 Pesaventob310efb2019-04-11 22:10:24 -040090 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 Zhang457f09a2018-01-05 13:45:38 -080095 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040096 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 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:
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 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
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800163} // namespace ndnsec
164} // namespace ndn