blob: 80a31ed8256f5949eefef867f01930ec00afe9ac [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 Afanasyevaf99f462015-01-19 21:43:09 -08003 * Copyright (c) 2013-2015 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
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_DSK_GEN_HPP
25#define NDN_TOOLS_NDNSEC_DSK_GEN_HPP
Yingdi Yu8d7468f2014-02-21 14:49:45 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080028
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';
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037
Alexander Afanasyevace74452014-11-30 22:28:24 -080038 po::options_description description("General Usage\n"
39 " ndnsec dsk-gen [-h] [-t keyType] identity\n"
40 "General options");
Yingdi Yub61f5402014-02-26 17:46:11 -080041 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("help,h", "produce help message")
Alexander Afanasyevace74452014-11-30 22:28:24 -080043 ("identity,i", po::value<std::string>(&identityName),
44 "identity name, for example, /ndn/ucla.edu/alice")
45 ("type,t", po::value<char>(&keyType)->default_value('r'),
46 "optional, key type, r for RSA key (default), e for ECDSA key.")
47 // ("size,s", po::value<int>(&keySize)->default_value(2048),
48 // "optional, key size, 2048 (default)")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080049 ;
50
51 po::positional_options_description p;
52 p.add("identity", 1);
53
54 po::variables_map vm;
Alexander Afanasyevace74452014-11-30 22:28:24 -080055 try {
56 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
57 po::notify(vm);
58 }
59 catch (const std::exception& e) {
60 std::cerr << "ERROR: " << e.what() << std::endl;
61 std::cerr << description << std::endl;
62 return 1;
63 }
Yingdi Yub61f5402014-02-26 17:46:11 -080064
Alexander Afanasyevace74452014-11-30 22:28:24 -080065 if (vm.count("help") != 0) {
66 std::cerr << description << std::endl;
67 return 0;
68 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069
Alexander Afanasyevace74452014-11-30 22:28:24 -080070 if (vm.count("identity") == 0) {
71 std::cerr << "identity must be specified" << std::endl;
72 std::cerr << description << std::endl;
73 return 1;
74 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080075
76 shared_ptr<IdentityCertificate> kskCert;
77 Name signingCertName;
Yingdi Yub61f5402014-02-26 17:46:11 -080078
79 KeyChain keyChain;
80
Alexander Afanasyevace74452014-11-30 22:28:24 -080081 try {
82 Name defaultCertName = keyChain.getDefaultCertificateNameForIdentity(identityName);
83 bool isDefaultDsk = false;
84 std::string keyUsageTag = defaultCertName.get(-3).toUri().substr(0,4);
85 if (keyUsageTag == "ksk-")
86 isDefaultDsk = false;
87 else if (keyUsageTag == "dsk-")
88 isDefaultDsk = true;
89 else {
90 std::cerr << "ERROR: Unknown key usage tag: " << keyUsageTag << std::endl;
91 return 1;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092 }
Alexander Afanasyevace74452014-11-30 22:28:24 -080093
94 if (isDefaultDsk) {
95 shared_ptr<IdentityCertificate> dskCert = keyChain.getCertificate(defaultCertName);
96
97 if (static_cast<bool>(dskCert)) {
98 SignatureSha256WithRsa sha256sig(dskCert->getSignature());
99
100 Name keyLocatorName = sha256sig.getKeyLocator().getName();
101
102 Name kskName = IdentityCertificate::certificateNameToPublicKeyName(keyLocatorName);
103 Name kskCertName = keyChain.getDefaultCertificateNameForKey(kskName);
104 signingCertName = kskCertName;
105 kskCert = keyChain.getCertificate(kskCertName);
106 }
107 else {
108 std::cerr << "ERROR: The default certificate is missing." << std::endl;
109 return 1;
110 }
111 }
112 else {
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 signingCertName = defaultCertName;
114 kskCert = keyChain.getCertificate(defaultCertName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800115 }
116
Alexander Afanasyevace74452014-11-30 22:28:24 -0800117 if (!static_cast<bool>(kskCert)) {
118 std::cerr << "ERROR: KSK certificate is missing." << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800119 return 1;
120 }
121
Alexander Afanasyevace74452014-11-30 22:28:24 -0800122 Name newKeyName;
123 switch (keyType) {
Yingdi Yub61f5402014-02-26 17:46:11 -0800124 case 'r':
125 {
Alexander Afanasyevace74452014-11-30 22:28:24 -0800126 RsaKeyParams params;
127 newKeyName = keyChain.generateRsaKeyPair(Name(identityName), false, params.getKeySize());
128 if (0 == newKeyName.size()) {
129 std::cerr << "ERROR: Fail to generate RSA key!" << std::endl;
130 return 1;
131 }
132 break;
133 }
134 case 'e':
135 {
136 EcdsaKeyParams params;
137 newKeyName = keyChain.generateEcdsaKeyPair(Name(identityName), false, params.getKeySize());
138 if (0 == newKeyName.size()) {
139 std::cerr << "ERROR: Fail to generate ECDSA key!" << std::endl;
140 return 1;
141 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800142 break;
143 }
144 default:
Alexander Afanasyevace74452014-11-30 22:28:24 -0800145 std::cerr << "ERROR: Unrecongized key type" << "\n";
Yingdi Yub61f5402014-02-26 17:46:11 -0800146 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800147 return 1;
148 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800149
Alexander Afanasyevace74452014-11-30 22:28:24 -0800150 Name certName = newKeyName.getPrefix(-1);
151 certName.append("KEY")
152 .append(newKeyName.get(-1))
153 .append("ID-CERT")
154 .appendVersion();
Yingdi Yub61f5402014-02-26 17:46:11 -0800155
Alexander Afanasyevace74452014-11-30 22:28:24 -0800156 shared_ptr<IdentityCertificate> certificate =
157 keyChain.prepareUnsignedIdentityCertificate(newKeyName,
158 Name(identityName),
159 kskCert->getNotBefore(),
160 kskCert->getNotAfter(),
161 kskCert->getSubjectDescriptionList());
Yingdi Yub61f5402014-02-26 17:46:11 -0800162
Alexander Afanasyevace74452014-11-30 22:28:24 -0800163 if (static_cast<bool>(certificate))
164 certificate->encode();
165 else {
166 std::cerr << "ERROR: Cannot format the certificate of the requested dsk." << "\n";
167 return 1;
168 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800169
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700170 keyChain.sign(*certificate,
171 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, signingCertName));
Yingdi Yub61f5402014-02-26 17:46:11 -0800172
Alexander Afanasyevace74452014-11-30 22:28:24 -0800173 keyChain.addCertificateAsIdentityDefault(*certificate);
Yingdi Yub61f5402014-02-26 17:46:11 -0800174
Alexander Afanasyevace74452014-11-30 22:28:24 -0800175 std::cerr << "OK: dsk certificate with name [" << certificate->getName() <<
176 "] has been successfully installed\n";
177 return 0;
178 }
179 catch (std::runtime_error& e) {
180 std::cerr << "ERROR: other runtime errors: " << e.what() << "\n";
181 return 1;
182 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800183}
184
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800185#endif // NDN_TOOLS_NDNSEC_DSK_GEN_HPP