blob: cd19ded84109d03e1b9db8a56bc2ae057146f2b5 [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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_KEY_GEN_HPP
25#define NDN_TOOLS_NDNSEC_KEY_GEN_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
30ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
35 std::string identityName;
Yingdi Yub61f5402014-02-26 17:46:11 -080036 bool isDefault = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 char keyType = 'r';
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 std::string outputFilename;
39
Yingdi Yuba8604d2014-10-13 19:03:12 -070040 po::options_description description("General Usage\n"
41 " ndnsec key-gen [-h] [-n] identity\n"
42 "General 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")
Yingdi Yuba8604d2014-10-13 19:03:12 -070045 ("identity,i", po::value<std::string>(&identityName),
46 "identity name, for example, /ndn/edu/ucla/alice")
47 ("not_default,n",
48 "optional, if not specified, the target identity will be set as "
49 "the default identity of the system")
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080050 ("dsk,d", "generate Data-Signing-Key (DSK) instead of the default Key-Signing-Key (KSK)")
Yingdi Yu7d8644a2014-12-01 22:55:49 -080051 ("type,t", po::value<char>(&keyType)->default_value('r'),
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070052 "optional, key type, r for RSA key (default), e for EC key")
Yingdi Yuba8604d2014-10-13 19:03:12 -070053 // ("size,s", po::value<int>(&keySize)->default_value(2048),
54 // "optional, key size, 2048 (default)")
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 {
62 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
63 vm);
64 po::notify(vm);
65 }
66 catch (const std::exception& e) {
67 std::cerr << "ERROR: " << e.what() << std::endl;
68 std::cerr << description << std::endl;
69 return 1;
70 }
Yingdi Yub61f5402014-02-26 17:46:11 -080071
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080072 if (vm.count("help") != 0) {
73 std::cerr << description << std::endl;
74 return 0;
75 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080076
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080077 if (vm.count("identity") == 0) {
78 std::cerr << "identity must be specified" << std::endl;
79 std::cerr << description << std::endl;
80 return 1;
81 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080082
Yingdi Yub61f5402014-02-26 17:46:11 -080083 if (vm.count("not_default") != 0)
84 isDefault = false;
85
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080086 bool isKsk = (vm.count("dsk") == 0);
87
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080088 ndn::security::v1::KeyChain keyChain;
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080089 Name keyName;
90
91 try {
92 switch (keyType) {
Yingdi Yub61f5402014-02-26 17:46:11 -080093 case 'r':
Yingdi Yu7d8644a2014-12-01 22:55:49 -080094 keyName = keyChain.generateRsaKeyPair(Name(identityName), isKsk, RsaKeyParams().getKeySize());
95 break;
96 case 'e':
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070097 keyName = keyChain.generateEcKeyPair(Name(identityName), isKsk, EcKeyParams().getKeySize());
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -080098 break;
Yingdi Yub61f5402014-02-26 17:46:11 -080099 default:
100 std::cerr << "Unrecongized key type" << "\n";
101 std::cerr << description << std::endl;
102 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103 }
104
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800105 if (0 == keyName.size()) {
106 std::cerr << "Error: failed to generate key" << "\n";
107 return 1;
108 }
109
110 keyChain.setDefaultKeyNameForIdentity(keyName);
111
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700112 shared_ptr<security::v1::IdentityCertificate> identityCert = keyChain.selfSign(keyName);
Alexander Afanasyev2a047eb2014-11-30 22:45:02 -0800113
114 if (isDefault)
115 keyChain.setDefaultIdentity(Name(identityName));
116
117 io::save(*identityCert, std::cout);
118 }
119 catch (const std::exception& e) {
120 std::cerr << "Error: " << e.what() << std::endl;
121 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800122 return 0;
123}
124
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800125#endif // NDN_TOOLS_NDNSEC_KEY_GEN_HPP