blob: 528852078e76e456f5e05a2a32341a8124cb565f [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_DSK_GEN_HPP
25#define NDNSEC_DSK_GEN_HPP
26
27#include "ndnsec-util.hpp"
28
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030ndnsec_dsk_gen(int argc, char** argv)
31{
32 using namespace ndn;
33 namespace po = boost::program_options;
34
35 std::string identityName;
36 char keyType = 'r';
37 int keySize = 2048;
38
Yingdi Yub61f5402014-02-26 17:46:11 -080039 po::options_description description("General Usage\n ndnsec dsk-gen [-h] identity\nGeneral options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
42 ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice")
43 // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)")
44 // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)")
45 ;
46
47 po::positional_options_description p;
48 p.add("identity", 1);
49
50 po::variables_map vm;
Yingdi Yub61f5402014-02-26 17:46:11 -080051 try
Yingdi Yu8d7468f2014-02-21 14:49:45 -080052 {
Yingdi Yub61f5402014-02-26 17:46:11 -080053 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
54 vm);
55 po::notify(vm);
56 }
57 catch (const std::exception& e)
58 {
59 std::cerr << "ERROR: " << e.what() << std::endl;
60 std::cerr << description << std::endl;
61 return 1;
62 }
63
64 if (vm.count("help") != 0)
65 {
66 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080067 return 0;
68 }
69
Yingdi Yub61f5402014-02-26 17:46:11 -080070 if (vm.count("identity") == 0)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 {
72 std::cerr << "identity must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -080073 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080074 return 1;
75 }
76
77 shared_ptr<IdentityCertificate> kskCert;
78 Name signingCertName;
Yingdi Yub61f5402014-02-26 17:46:11 -080079
80 KeyChain keyChain;
81
82 Name defaultCertName = keyChain.getDefaultCertificateNameForIdentity(identityName);
83 bool isDefaultDsk = false;
Alexander Afanasyev9c578182014-05-14 17:28:28 -070084 if (defaultCertName.get(-3).toUri().substr(0,4) == "dsk-")
Yingdi Yub61f5402014-02-26 17:46:11 -080085 isDefaultDsk = true;
86
87 if (isDefaultDsk)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080088 {
Yingdi Yub61f5402014-02-26 17:46:11 -080089 shared_ptr<IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName);
90 SignatureSha256WithRsa sha256sig(dskCert->getSignature());
Yingdi Yu8d7468f2014-02-21 14:49:45 -080091
Yingdi Yub61f5402014-02-26 17:46:11 -080092 Name keyLocatorName = sha256sig.getKeyLocator().getName(); // will throw exception if keylocator is absent or it is not a name
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093
Yingdi Yub61f5402014-02-26 17:46:11 -080094 Name kskName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
95 Name kskCertName = keyChain.getDefaultCertificateNameForKey(kskName);
96 signingCertName = kskCertName;
97 kskCert = keyChain.getCertificate(kskCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 }
Yingdi Yub61f5402014-02-26 17:46:11 -080099 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800100 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800101 signingCertName = defaultCertName;
102 kskCert = keyChain.getCertificate(defaultCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103 }
104
Yingdi Yub61f5402014-02-26 17:46:11 -0800105 if (!static_cast<bool>(kskCert))
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800106 {
107 std::cerr << "ERROR: no KSK certificate." << std::endl;
108 return 1;
109 }
110
Yingdi Yub61f5402014-02-26 17:46:11 -0800111 Name newKeyName;
112 switch (keyType)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800113 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800114 case 'r':
115 {
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700116 newKeyName = keyChain.generateRsaKeyPair(Name(identityName), false, keySize);
Yingdi Yub61f5402014-02-26 17:46:11 -0800117 if (0 == newKeyName.size())
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800118 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800119 std::cerr << "fail to generate key!" << std::endl;
120 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800121 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800122 break;
123 }
124 default:
125 std::cerr << "Unrecongized key type" << "\n";
126 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800127 return 1;
128 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800129
130 Name certName = newKeyName.getPrefix(-1);
131 certName.append("KEY")
132 .append(newKeyName.get(-1))
133 .append("ID-CERT")
134 .appendVersion();
135
136 shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>();
137 certificate->setName(certName);
138 certificate->setNotBefore(kskCert->getNotBefore());
139 certificate->setNotAfter(kskCert->getNotAfter());
140
141 certificate->setPublicKeyInfo(*keyChain.getPublicKey(newKeyName));
142
143 const std::vector<CertificateSubjectDescription>& subList =
144 kskCert->getSubjectDescriptionList();
145
146 for (std::vector<CertificateSubjectDescription>::const_iterator it = subList.begin();
147 it != subList.end(); it++)
148 certificate->addSubjectDescription(*it);
149
150 certificate->encode();
151
152 keyChain.sign(*certificate, signingCertName);
153
154 keyChain.addCertificateAsIdentityDefault(*certificate);
155
156 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800157}
158
159#endif //NDNSEC_DSK_GEN_HPP