blob: 00f766746e1e4f218b95cf547a46ed2ed83e23b2 [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/*
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08004 *
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
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050025#include <boost/tokenizer.hpp>
26
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080027namespace ndn {
28namespace ndnsec {
29
30int
31ndnsec_cert_gen(int argc, char** argv)
32{
33 using boost::tokenizer;
34 using boost::escaped_list_separator;
35
36 namespace po = boost::program_options;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080037 namespace t = security::transform;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080038
Alexander Afanasyev35109a12017-01-04 15:39:06 -080039 security::v2::KeyChain keyChain;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080040
41 std::string notBeforeStr;
42 std::string notAfterStr;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080043 std::string requestFile("-");
44 Name signId;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080045 std::vector<std::string> infos;
46 std::string issuerId;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080047
48 po::options_description description(
49 "General Usage\n"
Alexander Afanasyev35109a12017-01-04 15:39:06 -080050 " ndnsec cert-gen [-h] [-S date] [-E date] [-I info] [-s sign-id] request\n"
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080051 "General options");
52
53 description.add_options()
54 ("help,h", "produce help message")
55 ("not-before,S", po::value<std::string>(&notBeforeStr),
56 "certificate starting date, YYYYMMDDhhmmss (default: now)")
57 ("not-after,E", po::value<std::string>(&notAfterStr),
58 "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080059 ("info,I", po::value<std::vector<std::string>>(&infos),
60 "key and value (must be separated by a single space) of the additional "
61 "description to be included in the issued certificate, e.g., "
62 "\"affiliation University of California, Los Angeles\". "
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 "May be repeated multiple times")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080064 ("sign-id,s", po::value<Name>(&signId),
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080065 "signing identity")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080066 ("request,r", po::value<std::string>(&requestFile)->default_value("-"),
67 "request file name, - for stdin")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080068 ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"),
69 "issuer's ID to be included as part of the issued certificate name")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080070 ;
71
72 po::positional_options_description p;
73 p.add("request", 1);
74
75 po::variables_map vm;
76 try {
77 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
78 po::notify(vm);
79 }
80 catch (const std::exception& e) {
81 std::cerr << "ERROR: " << e.what() << std::endl;
82 return 1;
83 }
84
85 if (vm.count("help") != 0) {
86 std::cout << description << std::endl;
87 return 0;
88 }
89
Alexander Afanasyev35109a12017-01-04 15:39:06 -080090 security::v2::AdditionalDescription additionalDescription;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080091
Alexander Afanasyev35109a12017-01-04 15:39:06 -080092 for (const auto& info : infos) {
93 size_t pos = info.find(" ");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080094 if (pos == std::string::npos) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080095 std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080096 return 1;
97 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080098 std::string key = info.substr(0, pos);
99 std::string value = info.substr(pos + 1);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800100
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800101 additionalDescription.set(key, value);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800102 }
103
104 time::system_clock::TimePoint notBefore;
105 time::system_clock::TimePoint notAfter;
106
107 if (vm.count("not-before") == 0) {
108 notBefore = time::system_clock::now();
109 }
110 else {
111 notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
112 }
113
114 if (vm.count("not-after") == 0) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 notAfter = notBefore + 365_days;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800116 }
117 else {
118 notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
119
120 if (notAfter < notBefore) {
121 std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl
122 << std::endl
123 << description << std::endl;
124 return 1;
125 }
126 }
127
128 if (vm.count("request") == 0) {
129 std::cerr << "ERROR: request file must be specified" << std::endl
130 << std::endl
131 << description << std::endl;
132 return 1;
133 }
134
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800135 security::v2::Certificate certRequest = loadCertificate(requestFile);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800136
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800137 // validate that the content is a public key
138 try {
139 Buffer keyContent = certRequest.getPublicKey();
140 t::PublicKey pubKey;
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400141 pubKey.loadPkcs8(keyContent.data(), keyContent.size());
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800142 }
143 catch (const std::exception& e) {
144 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800145 return 1;
146 }
147
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800148 security::v2::Certificate cert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800149
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800150 Name certName = certRequest.getKeyName();
151 certName
152 .append(issuerId)
153 .appendVersion();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800154
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800155 cert.setName(certName);
156 cert.setContent(certRequest.getContent());
157
158 // @TODO add ability to customize
Davide Pesavento0f830802018-01-16 23:58:58 -0500159 cert.setFreshnessPeriod(1_h);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800160
161 SignatureInfo signatureInfo;
162 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
Alexander Afanasyev464da532017-09-02 12:16:32 -0400163 if (!additionalDescription.empty()) {
164 signatureInfo.appendTypeSpecificTlv(additionalDescription.wireEncode());
165 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800166
167 security::Identity identity;
168 if (vm.count("sign-id") == 0) {
169 identity = keyChain.getPib().getDefaultIdentity();
170 }
171 else {
172 identity = keyChain.getPib().getIdentity(signId);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800173 }
174
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800175 keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800176
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800177 Block wire = cert.wireEncode();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800178
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800179
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800180 try {
181 t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout);
182 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800183 catch (const t::Error& e) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800184 std::cerr << "ERROR: " << e.what() << std::endl;
185 return 1;
186 }
187
188 return 0;
189}
190
191} // namespace ndnsec
192} // namespace ndn