blob: 6d4e3977f41b0250e880000eae83464d01683912 [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 Pesaventob310efb2019-04-11 22:10:24 -04003 * Copyright (c) 2013-2019 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(
40 "Usage: ndnsec key-gen [-h] [-n] [-t TYPE] [-k IDTYPE] [-i] IDENTITY\n"
41 "\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")
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 Yu8d7468f2014-02-21 14:49:45 -080053 ;
54
55 po::positional_options_description p;
56 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080057
Yingdi Yub61f5402014-02-26 17:46:11 -080058 po::variables_map vm;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080059 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080060 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080061 po::notify(vm);
62 }
63 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040064 std::cerr << "ERROR: " << e.what() << "\n\n"
65 << description << std::endl;
66 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080067 }
Yingdi Yub61f5402014-02-26 17:46:11 -080068
Davide Pesaventob310efb2019-04-11 22:10:24 -040069 if (vm.count("help") > 0) {
70 std::cout << description << std::endl;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080071 return 0;
72 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080073
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080074 if (vm.count("identity") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040075 std::cerr << "ERROR: you must specify an identity" << std::endl;
76 return 2;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080077 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078
Davide Pesaventob310efb2019-04-11 22:10:24 -040079 KeyIdType keyIdType = KeyIdType::RANDOM;
80 Name::Component userKeyIdComponent;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080081
Davide Pesaventob310efb2019-04-11 22:10:24 -040082 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 Zhang457f09a2018-01-05 13:45:38 -080088 }
Davide Pesaventob310efb2019-04-11 22:10:24 -040089 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 Zhang457f09a2018-01-05 13:45:38 -0800106 keyIdType = KeyIdType::SHA256;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400107 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 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 Pesaventob310efb2019-04-11 22:10:24 -0400137 security::v2::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