blob: 48646d14060e68721030c27b62bf9cbeedc604f7 [file] [log] [blame]
Yingdi Yue6bfab22014-02-06 16:01:19 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include <iostream>
12#include <fstream>
13
14#include <boost/program_options/options_description.hpp>
15#include <boost/program_options/variables_map.hpp>
16#include <boost/program_options/parsers.hpp>
17
18#include <cryptopp/base64.h>
19#include <cryptopp/files.h>
20
21#include "security/key-chain.hpp"
22
23using namespace std;
24using namespace ndn;
25namespace po = boost::program_options;
26
27
28int main(int argc, char** argv)
29{
30 string identityName;
31 bool dskFlag = false;
32 bool notDefault = false;
33 char keyType = 'r';
34 int keySize = 2048;
35 string outputFilename;
36
37 // po::options_description desc("General Usage\n ndn-keygen [-h] [-d] [-i] [-t type] [-s size] identity\nGeneral options");
38 po::options_description desc("General Usage\n ndn-keygen [-h] [-i] identity\nGeneral options");
39 desc.add_options()
40 ("help,h", "produce help message")
41 ("identity_name,n", po::value<string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice")
42 // ("dsk,d", "optional, if specified, a Data-Signing-Key will be created, otherwise create a Key-Signing-Key")
43 ("not_default,i", "optional, if not specified, the target identity will be set as the default identity of the system")
44 // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)")
45 // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)")
46 ;
47
48 po::positional_options_description p;
49 p.add("identity_name", 1);
50
51 po::variables_map vm;
52 po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
53 po::notify(vm);
54
55 if (vm.count("help"))
56 {
57 cerr << desc << endl;
58 return 1;
59 }
60
61 if (0 == vm.count("identity_name"))
62 {
63 cerr << "identity_name must be specified" << endl;
64 cerr << desc << endl;
65 return 1;
66 }
67
68 // if (vm.count("dsk"))
69 // dskFlag = true;
70
71 if (vm.count("not_default"))
72 notDefault = true;
73
74 KeyChain keyChain;
75
76 // if (vm.count("type"))
77 if (true)
78 {
79 switch(keyType)
80 {
81 case 'r':
82 {
83 try
84 {
85 Name keyName = keyChain.generateRSAKeyPair(Name(identityName), !dskFlag, keySize);
86
87 if(0 == keyName.size())
88 {
89 return 1;
90 }
91
92 keyChain.setDefaultKeyNameForIdentity(keyName);
93
94 ptr_lib::shared_ptr<IdentityCertificate> idcert = keyChain.selfSign(keyName);
95
96 if(!notDefault)
97 {
98 keyChain.setDefaultIdentity(Name(identityName));
99 }
100
101 CryptoPP::StringSource ss(idcert->wireEncode().wire(),
102 idcert->wireEncode().size(),
103 true,
104 new CryptoPP::Base64Encoder(new CryptoPP::FileSink(cout), true, 64));
105 return 0;
106 }
107 catch(std::exception &e)
108 {
109 cerr << "ERROR: " << e.what() << endl;
110 return 1;
111 }
112 }
113 default:
114 cerr << "Unrecongized key type" << "\n";
115 cerr << desc << endl;
116 return 1;
117 }
118 }
119
120 return 0;
121}