blob: dbab9cb634c7a6c3480d3eb3e0a5940339d14208 [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev464da532017-09-02 12:16:32 -04002/*
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
22#include "ndnsec.hpp"
23#include "util.hpp"
24
25namespace ndn {
26namespace ndnsec {
27
28int
29ndnsec_cert_gen(int argc, char** argv)
30{
31 using boost::tokenizer;
32 using boost::escaped_list_separator;
33
34 namespace po = boost::program_options;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080035 namespace t = security::transform;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080036
Alexander Afanasyev35109a12017-01-04 15:39:06 -080037 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038
39 std::string notBeforeStr;
40 std::string notAfterStr;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080041 std::string requestFile("-");
42 Name signId;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080043 std::vector<std::string> infos;
44 std::string issuerId;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080045
46 po::options_description description(
47 "General Usage\n"
Alexander Afanasyev35109a12017-01-04 15:39:06 -080048 " ndnsec cert-gen [-h] [-S date] [-E date] [-I info] [-s sign-id] request\n"
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080049 "General options");
50
51 description.add_options()
52 ("help,h", "produce help message")
53 ("not-before,S", po::value<std::string>(&notBeforeStr),
54 "certificate starting date, YYYYMMDDhhmmss (default: now)")
55 ("not-after,E", po::value<std::string>(&notAfterStr),
56 "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080057 ("info,I", po::value<std::vector<std::string>>(&infos),
58 "key and value (must be separated by a single space) of the additional "
59 "description to be included in the issued certificate, e.g., "
60 "\"affiliation University of California, Los Angeles\". "
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080061 "May be repeated multiple times")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080062 ("sign-id,s", po::value<Name>(&signId),
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 "signing identity")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080064 ("request,r", po::value<std::string>(&requestFile)->default_value("-"),
65 "request file name, - for stdin")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080066 ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"),
67 "issuer's ID to be included as part of the issued certificate name")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080068 ;
69
70 po::positional_options_description p;
71 p.add("request", 1);
72
73 po::variables_map vm;
74 try {
75 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
76 po::notify(vm);
77 }
78 catch (const std::exception& e) {
79 std::cerr << "ERROR: " << e.what() << std::endl;
80 return 1;
81 }
82
83 if (vm.count("help") != 0) {
84 std::cout << description << std::endl;
85 return 0;
86 }
87
Alexander Afanasyev35109a12017-01-04 15:39:06 -080088 security::v2::AdditionalDescription additionalDescription;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080089
Alexander Afanasyev35109a12017-01-04 15:39:06 -080090 for (const auto& info : infos) {
91 size_t pos = info.find(" ");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092 if (pos == std::string::npos) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080093 std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094 return 1;
95 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096 std::string key = info.substr(0, pos);
97 std::string value = info.substr(pos + 1);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080098
Alexander Afanasyev35109a12017-01-04 15:39:06 -080099 additionalDescription.set(key, value);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800100 }
101
102 time::system_clock::TimePoint notBefore;
103 time::system_clock::TimePoint notAfter;
104
105 if (vm.count("not-before") == 0) {
106 notBefore = time::system_clock::now();
107 }
108 else {
109 notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
110 }
111
112 if (vm.count("not-after") == 0) {
113 notAfter = notBefore + time::days(365);
114 }
115 else {
116 notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
117
118 if (notAfter < notBefore) {
119 std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl
120 << std::endl
121 << description << std::endl;
122 return 1;
123 }
124 }
125
126 if (vm.count("request") == 0) {
127 std::cerr << "ERROR: request file must be specified" << std::endl
128 << std::endl
129 << description << std::endl;
130 return 1;
131 }
132
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800133 security::v2::Certificate certRequest = loadCertificate(requestFile);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800134
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800135 // validate that the content is a public key
136 try {
137 Buffer keyContent = certRequest.getPublicKey();
138 t::PublicKey pubKey;
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400139 pubKey.loadPkcs8(keyContent.data(), keyContent.size());
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800140 }
141 catch (const std::exception& e) {
142 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800143 return 1;
144 }
145
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800146 security::v2::Certificate cert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800147
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800148 Name certName = certRequest.getKeyName();
149 certName
150 .append(issuerId)
151 .appendVersion();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800152
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800153 cert.setName(certName);
154 cert.setContent(certRequest.getContent());
155
156 // @TODO add ability to customize
157 cert.setFreshnessPeriod(time::hours(1));
158
159 SignatureInfo signatureInfo;
160 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
Alexander Afanasyev464da532017-09-02 12:16:32 -0400161 if (!additionalDescription.empty()) {
162 signatureInfo.appendTypeSpecificTlv(additionalDescription.wireEncode());
163 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800164
165 security::Identity identity;
166 if (vm.count("sign-id") == 0) {
167 identity = keyChain.getPib().getDefaultIdentity();
168 }
169 else {
170 identity = keyChain.getPib().getIdentity(signId);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800171 }
172
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800173 keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800174
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800175 Block wire = cert.wireEncode();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800176
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800177
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800178 try {
179 t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout);
180 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800181 catch (const t::Error& e) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800182 std::cerr << "ERROR: " << e.what() << std::endl;
183 return 1;
184 }
185
186 return 0;
187}
188
189} // namespace ndnsec
190} // namespace ndn