blob: b74ccc2bc373cbef37a48697bfdf4f681e6ee3c7 [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
24#ifndef NDNSEC_KEY_GEN_HPP
25#define NDNSEC_KEY_GEN_HPP
26
27#include "ndnsec-util.hpp"
28
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';
38 int keySize = 2048;
39 std::string outputFilename;
40
Yingdi Yuba8604d2014-10-13 19:03:12 -070041 po::options_description description("General Usage\n"
42 " ndnsec key-gen [-h] [-n] identity\n"
43 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080044 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045 ("help,h", "produce help message")
Yingdi Yuba8604d2014-10-13 19:03:12 -070046 ("identity,i", po::value<std::string>(&identityName),
47 "identity name, for example, /ndn/edu/ucla/alice")
48 ("not_default,n",
49 "optional, if not specified, the target identity will be set as "
50 "the default identity of the system")
51 // ("type,t", po::value<char>(&keyType)->default_value('r'),
52 // "optional, key type, r for RSA key (default)")
53 // ("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;
61 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080062 {
Yingdi Yub61f5402014-02-26 17:46:11 -080063 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
64 vm);
65 po::notify(vm);
66 }
67 catch (const std::exception& e)
68 {
69 std::cerr << "ERROR: " << e.what() << std::endl;
70 std::cerr << description << std::endl;
71 return 1;
72 }
73
74 if (vm.count("help") != 0)
75 {
76 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080077 return 0;
78 }
79
Yingdi Yub61f5402014-02-26 17:46:11 -080080 if (vm.count("identity") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080081 {
82 std::cerr << "identity must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080083 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080084 return 1;
85 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086
Yingdi Yub61f5402014-02-26 17:46:11 -080087 if (vm.count("not_default") != 0)
88 isDefault = false;
89
90 switch (keyType)
91 {
92 case 'r':
93 {
94 shared_ptr<IdentityCertificate> identityCert;
95
96 KeyChain keyChain;
97
Yingdi Yuf56c68f2014-04-24 21:50:13 -070098 Name keyName = keyChain.generateRsaKeyPair(Name(identityName), true, keySize);
Yingdi Yub61f5402014-02-26 17:46:11 -080099
100 if (0 == keyName.size())
101 return 1;
102
103 keyChain.setDefaultKeyNameForIdentity(keyName);
104
105 identityCert = keyChain.selfSign(keyName);
106
107 if (isDefault)
108 keyChain.setDefaultIdentity(Name(identityName));
109
110 io::save(*identityCert, std::cout);
111 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800112 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 default:
114 std::cerr << "Unrecongized key type" << "\n";
115 std::cerr << description << std::endl;
116 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800117 }
118
119 return 0;
120}
121
122#endif //NDNSEC_KEY_GEN_HPP