blob: c1f8868828d875cac61a98ad2d5286e7f13a42df [file] [log] [blame]
Alexander Afanasyev82c359c2017-01-04 14:48:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * 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
88 if (vm.count("subject-name") == 0) {
89 std::cerr << "ERROR: subject name must be specified" << std::endl
90 << std::endl
91 << description << std::endl;
92 return 1;
93 }
94
Alexander Afanasyev35109a12017-01-04 15:39:06 -080095 security::v2::AdditionalDescription additionalDescription;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080096
Alexander Afanasyev35109a12017-01-04 15:39:06 -080097 for (const auto& info : infos) {
98 size_t pos = info.find(" ");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080099 if (pos == std::string::npos) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800100 std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800101 return 1;
102 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800103 std::string key = info.substr(0, pos);
104 std::string value = info.substr(pos + 1);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800105
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800106 additionalDescription.set(key, value);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800107 }
108
109 time::system_clock::TimePoint notBefore;
110 time::system_clock::TimePoint notAfter;
111
112 if (vm.count("not-before") == 0) {
113 notBefore = time::system_clock::now();
114 }
115 else {
116 notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
117 }
118
119 if (vm.count("not-after") == 0) {
120 notAfter = notBefore + time::days(365);
121 }
122 else {
123 notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
124
125 if (notAfter < notBefore) {
126 std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl
127 << std::endl
128 << description << std::endl;
129 return 1;
130 }
131 }
132
133 if (vm.count("request") == 0) {
134 std::cerr << "ERROR: request file must be specified" << std::endl
135 << std::endl
136 << description << std::endl;
137 return 1;
138 }
139
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800140 security::v2::Certificate certRequest = loadCertificate(requestFile);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800141
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800142 // validate that the content is a public key
143 try {
144 Buffer keyContent = certRequest.getPublicKey();
145 t::PublicKey pubKey;
146 pubKey.loadPkcs8(keyContent.buf(), keyContent.size());
147 }
148 catch (const std::exception& e) {
149 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800150 return 1;
151 }
152
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800153 security::v2::Certificate cert;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800154
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800155 Name certName = certRequest.getKeyName();
156 certName
157 .append(issuerId)
158 .appendVersion();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800159
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800160 cert.setName(certName);
161 cert.setContent(certRequest.getContent());
162
163 // @TODO add ability to customize
164 cert.setFreshnessPeriod(time::hours(1));
165
166 SignatureInfo signatureInfo;
167 signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter));
168
169 security::Identity identity;
170 if (vm.count("sign-id") == 0) {
171 identity = keyChain.getPib().getDefaultIdentity();
172 }
173 else {
174 identity = keyChain.getPib().getIdentity(signId);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800175 }
176
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800177 keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800178
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800179 Block wire = cert.wireEncode();
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800180
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800181
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800182 try {
183 t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout);
184 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800185 catch (const t::Error& e) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800186 std::cerr << "ERROR: " << e.what() << std::endl;
187 return 1;
188 }
189
190 return 0;
191}
192
193} // namespace ndnsec
194} // namespace ndn