blob: 76aa69c1f9ce87e65cd7e17320e08d3ab42223bd [file] [log] [blame]
Yingdi Yu8d7468f2014-02-21 14:49:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * BSD license, See the LICENSE file for more information
5 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
8#ifndef NDNSEC_KEY_GEN_HPP
9#define NDNSEC_KEY_GEN_HPP
10
11#include "ndnsec-util.hpp"
12
Yingdi Yub61f5402014-02-26 17:46:11 -080013int
14ndnsec_key_gen(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080015{
16 using namespace ndn;
17 namespace po = boost::program_options;
18
19 std::string identityName;
Yingdi Yub61f5402014-02-26 17:46:11 -080020 bool isDefault = true;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080021 char keyType = 'r';
22 int keySize = 2048;
23 std::string outputFilename;
24
Yingdi Yub61f5402014-02-26 17:46:11 -080025 po::options_description description("General Usage\n ndnsec key-gen [-h] [-n] identity\nGeneral options");
26 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027 ("help,h", "produce help message")
28 ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice")
29 ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system")
30 // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)")
31 // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)")
32 ;
33
34 po::positional_options_description p;
35 p.add("identity", 1);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036
Yingdi Yub61f5402014-02-26 17:46:11 -080037 po::variables_map vm;
38 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080039 {
Yingdi Yub61f5402014-02-26 17:46:11 -080040 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
41 vm);
42 po::notify(vm);
43 }
44 catch (const std::exception& e)
45 {
46 std::cerr << "ERROR: " << e.what() << std::endl;
47 std::cerr << description << std::endl;
48 return 1;
49 }
50
51 if (vm.count("help") != 0)
52 {
53 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080054 return 0;
55 }
56
Yingdi Yub61f5402014-02-26 17:46:11 -080057 if (vm.count("identity") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080058 {
59 std::cerr << "identity must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080060 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080061 return 1;
62 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080063
Yingdi Yub61f5402014-02-26 17:46:11 -080064 if (vm.count("not_default") != 0)
65 isDefault = false;
66
67 switch (keyType)
68 {
69 case 'r':
70 {
71 shared_ptr<IdentityCertificate> identityCert;
72
73 KeyChain keyChain;
74
75 Name keyName = keyChain.generateRSAKeyPair(Name(identityName), true, keySize);
76
77 if (0 == keyName.size())
78 return 1;
79
80 keyChain.setDefaultKeyNameForIdentity(keyName);
81
82 identityCert = keyChain.selfSign(keyName);
83
84 if (isDefault)
85 keyChain.setDefaultIdentity(Name(identityName));
86
87 io::save(*identityCert, std::cout);
88 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080089 }
Yingdi Yub61f5402014-02-26 17:46:11 -080090 default:
91 std::cerr << "Unrecongized key type" << "\n";
92 std::cerr << description << std::endl;
93 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080094 }
95
96 return 0;
97}
98
99#endif //NDNSEC_KEY_GEN_HPP