blob: 584b79ceda41fddadb5295c6c2f00661627f1c00 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 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{
31 using namespace ndn;
32 namespace po = boost::program_options;
33
34 std::string identityName;
Yingdi Yub61f5402014-02-26 17:46:11 -080035 bool isDefault = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 char keyType = 'r';
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 std::string outputFilename;
38
Yingdi Yuba8604d2014-10-13 19:03:12 -070039 po::options_description description("General Usage\n"
40 " ndnsec key-gen [-h] [-n] identity\n"
41 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080042 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043 ("help,h", "produce help message")
Yingdi Yuba8604d2014-10-13 19:03:12 -070044 ("identity,i", po::value<std::string>(&identityName),
45 "identity name, for example, /ndn/edu/ucla/alice")
46 ("not_default,n",
47 "optional, if not specified, the target identity will be set as "
48 "the default identity of the system")
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080049 ("dsk,d", "generate Data-Signing-Key (DSK) instead of the default Key-Signing-Key (KSK)")
Yingdi Yu7d8644a2014-12-01 22:55:49 -080050 ("type,t", po::value<char>(&keyType)->default_value('r'),
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070051 "optional, key type, r for RSA key (default), e for EC key")
Yingdi Yuba8604d2014-10-13 19:03:12 -070052 // ("size,s", po::value<int>(&keySize)->default_value(2048),
53 // "optional, key size, 2048 (default)")
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) {
65 std::cerr << "ERROR: " << e.what() << std::endl;
66 std::cerr << description << std::endl;
67 return 1;
68 }
Yingdi Yub61f5402014-02-26 17:46:11 -080069
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080070 if (vm.count("help") != 0) {
71 std::cerr << description << std::endl;
72 return 0;
73 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080075 if (vm.count("identity") == 0) {
76 std::cerr << "identity must be specified" << std::endl;
77 std::cerr << description << std::endl;
78 return 1;
79 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080080
Yingdi Yub61f5402014-02-26 17:46:11 -080081 if (vm.count("not_default") != 0)
82 isDefault = false;
83
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080084 bool isKsk = (vm.count("dsk") == 0);
85
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080086 security::v1::KeyChain keyChain;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080087 Name keyName;
88
89 try {
90 switch (keyType) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080091 case 'r':
92 keyName = keyChain.generateRsaKeyPair(Name(identityName), isKsk, RsaKeyParams().getKeySize());
93 break;
94 case 'e':
95 keyName = keyChain.generateEcKeyPair(Name(identityName), isKsk, EcKeyParams().getKeySize());
96 break;
97 default:
98 std::cerr << "Unrecongized key type\n"
99 << description << std::endl;
100 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800101 }
102
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800103 if (keyName.empty()) {
104 std::cerr << "Error: failed to generate key" << std::endl;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800105 return 1;
106 }
107
108 keyChain.setDefaultKeyNameForIdentity(keyName);
109
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700110 shared_ptr<security::v1::IdentityCertificate> identityCert = keyChain.selfSign(keyName);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800111
112 if (isDefault)
113 keyChain.setDefaultIdentity(Name(identityName));
114
115 io::save(*identityCert, std::cout);
116 }
117 catch (const std::exception& e) {
118 std::cerr << "Error: " << e.what() << std::endl;
119 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800120 return 0;
121}
122
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800123} // namespace ndnsec
124} // namespace ndn