blob: 49b0362eaab8687663121c0769ce064c0ccf7610 [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 Pesavento258d51a2022-02-27 21:26:28 -05003 * Copyright (c) 2013-2022 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
Alexander Afanasyev09236c22020-06-03 13:42:38 -040025#include "ndn-cxx/security/additional-description.hpp"
Davide Pesaventob310efb2019-04-11 22:10:24 -040026#include "ndn-cxx/security/transform/base64-encode.hpp"
27#include "ndn-cxx/security/transform/buffer-source.hpp"
28#include "ndn-cxx/security/transform/public-key.hpp"
29#include "ndn-cxx/security/transform/stream-sink.hpp"
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050030
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031namespace ndn {
32namespace ndnsec {
33
34int
35ndnsec_cert_gen(int argc, char** argv)
36{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080037 namespace po = boost::program_options;
38
Davide Pesaventob310efb2019-04-11 22:10:24 -040039 std::string requestFile;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080040 std::string notBeforeStr;
41 std::string notAfterStr;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080042 std::vector<std::string> infos;
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 Name signId;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080044 std::string issuerId;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080045
46 po::options_description description(
Davide Pesaventob310efb2019-04-11 22:10:24 -040047 "Usage: ndnsec cert-gen [-h] [-S TIMESTAMP] [-E TIMESTAMP] [-I INFO]...\n"
48 " [-s IDENTITY] [-i ISSUER] [-r] FILE\n"
49 "\n"
50 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080051 description.add_options()
52 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040053 ("request,r", po::value<std::string>(&requestFile)->default_value("-"),
54 "request file name, '-' for stdin (the default)")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080055 ("not-before,S", po::value<std::string>(&notBeforeStr),
Davide Pesaventob310efb2019-04-11 22:10:24 -040056 "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080057 ("not-after,E", po::value<std::string>(&notAfterStr),
Davide Pesaventob310efb2019-04-11 22:10:24 -040058 "certificate validity end date/time in YYYYMMDDhhmmss format (default: "
59 "365 days after the --not-before timestamp)")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080060 ("info,I", po::value<std::vector<std::string>>(&infos),
61 "key and value (must be separated by a single space) of the additional "
Davide Pesaventob310efb2019-04-11 22:10:24 -040062 "description to be included in the issued certificate (e.g., "
63 "\"affiliation University of California, Los Angeles\"); "
64 "this option may be repeated multiple times")
65 ("sign-id,s", po::value<Name>(&signId), "signing identity")
Junxiao Shi9ee770b2022-04-25 23:33:33 +000066 ("issuer-id,i", po::value<std::string>(&issuerId),
67 ("issuer's ID to be included in the issued certificate name, interpreted as "
68 "name component in URI format (default: \"" +
69 security::Certificate::DEFAULT_ISSUER_ID.toUri() + "\")").data())
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) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040081 std::cerr << "ERROR: " << e.what() << "\n\n"
82 << description << std::endl;
83 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 }
85
Davide Pesaventob310efb2019-04-11 22:10:24 -040086 if (vm.count("help") > 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080087 std::cout << description << std::endl;
88 return 0;
89 }
90
Davide Pesaventof2cae612021-03-24 18:47:05 -040091 security::AdditionalDescription additionalDescription;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080092
Alexander Afanasyev35109a12017-01-04 15:39:06 -080093 for (const auto& info : infos) {
Davide Pesaventof2cae612021-03-24 18:47:05 -040094 auto pos = info.find(' ');
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080095 if (pos == std::string::npos) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096 std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl;
Davide Pesaventob310efb2019-04-11 22:10:24 -040097 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080098 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080099 std::string key = info.substr(0, pos);
100 std::string value = info.substr(pos + 1);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800101
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800102 additionalDescription.set(key, value);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800103 }
104
105 time::system_clock::TimePoint notBefore;
106 time::system_clock::TimePoint notAfter;
107
108 if (vm.count("not-before") == 0) {
109 notBefore = time::system_clock::now();
110 }
111 else {
112 notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
113 }
114
115 if (vm.count("not-after") == 0) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500116 notAfter = notBefore + 365_days;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800117 }
118 else {
119 notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
120
121 if (notAfter < notBefore) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400122 std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl;
123 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800124 }
125 }
126
Davide Pesaventof2cae612021-03-24 18:47:05 -0400127 KeyChain keyChain;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400128
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000129 auto request = loadFromFile<security::Certificate>(requestFile);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800130
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000131 security::SigningInfo signer;
132 if (vm.count("sign-id") > 0) {
133 signer.setSigningIdentity(signId);
134 }
Alexander Afanasyev464da532017-09-02 12:16:32 -0400135 if (!additionalDescription.empty()) {
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000136 SignatureInfo sigInfo;
137 sigInfo.addCustomTlv(additionalDescription.wireEncode());
138 signer.setSignatureInfo(sigInfo);
Alexander Afanasyev464da532017-09-02 12:16:32 -0400139 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800140
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000141 security::MakeCertificateOptions opts;
142 if (vm.count("issuer-id") > 0) {
143 opts.issuerId = name::Component::fromEscapedString(issuerId);
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800144 }
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000145 opts.validity.emplace(notBefore, notAfter);
146 auto cert = keyChain.makeCertificate(request, signer, opts);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800147
Davide Pesaventob310efb2019-04-11 22:10:24 -0400148 {
149 using namespace security::transform;
Davide Pesavento258d51a2022-02-27 21:26:28 -0500150 bufferSource(cert.wireEncode()) >> base64Encode(true) >> streamSink(std::cout);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800151 }
152
153 return 0;
154}
155
156} // namespace ndnsec
157} // namespace ndn