blob: 4781ddfbb9c02aa7309a328ff2f19602a30a6839 [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu8d7468f2014-02-21 14:49:45 -080013 */
14
15#ifndef NDNSEC_KEY_GEN_HPP
16#define NDNSEC_KEY_GEN_HPP
17
18#include "ndnsec-util.hpp"
19
Yingdi Yub61f5402014-02-26 17:46:11 -080020int
21ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080022{
23 using namespace ndn;
24 namespace po = boost::program_options;
25
26 std::string identityName;
Yingdi Yub61f5402014-02-26 17:46:11 -080027 bool isDefault = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028 char keyType = 'r';
29 int keySize = 2048;
30 std::string outputFilename;
31
Yingdi Yub61f5402014-02-26 17:46:11 -080032 po::options_description description("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options");
33 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 ("help,h", "produce help message")
35 ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice")
36 ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system")
37 // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)")
38 // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)")
39 ;
40
41 po::positional_options_description p;
42 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080043
Yingdi Yub61f5402014-02-26 17:46:11 -080044 po::variables_map vm;
45 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046 {
Yingdi Yub61f5402014-02-26 17:46:11 -080047 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
48 vm);
49 po::notify(vm);
50 }
51 catch (const std::exception& e)
52 {
53 std::cerr << "ERROR: " << e.what() << std::endl;
54 std::cerr << description << std::endl;
55 return 1;
56 }
57
58 if (vm.count("help") != 0)
59 {
60 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 return 0;
62 }
63
Yingdi Yub61f5402014-02-26 17:46:11 -080064 if (vm.count("identity") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065 {
66 std::cerr << "identity must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080067 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080068 return 1;
69 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070
Yingdi Yub61f5402014-02-26 17:46:11 -080071 if (vm.count("not_default") != 0)
72 isDefault = false;
73
74 switch (keyType)
75 {
76 case 'r':
77 {
78 shared_ptr<IdentityCertificate> identityCert;
79
80 KeyChain keyChain;
81
82 Name keyName = keyChain.generateRSAKeyPair(Name(identityName), true, keySize);
83
84 if (0 == keyName.size())
85 return 1;
86
87 keyChain.setDefaultKeyNameForIdentity(keyName);
88
89 identityCert = keyChain.selfSign(keyName);
90
91 if (isDefault)
92 keyChain.setDefaultIdentity(Name(identityName));
93
94 io::save(*identityCert, std::cout);
95 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080096 }
Yingdi Yub61f5402014-02-26 17:46:11 -080097 default:
98 std::cerr << "Unrecongized key type" << "\n";
99 std::cerr << description << std::endl;
100 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800101 }
102
103 return 0;
104}
105
106#endif //NDNSEC_KEY_GEN_HPP