blob: c38211919194b0daf8edb12e6996371619a9d896 [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 Pesavento187e9f92023-03-20 22:46:22 -04003 * Copyright (c) 2013-2023 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"
Davide Pesaventob310efb2019-04-11 22:10:24 -040028#include "ndn-cxx/security/transform/stream-sink.hpp"
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031
32int
33ndnsec_cert_gen(int argc, char** argv)
34{
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035 namespace po = boost::program_options;
Davide Pesavento58de07a2022-06-08 00:02:02 -040036 using security::Certificate;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080037
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 std::string requestFile;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080039 std::string notBeforeStr;
40 std::string notAfterStr;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080041 std::vector<std::string> infos;
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 Name signId;
Davide Pesavento58de07a2022-06-08 00:02:02 -040043 std::string issuer;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080044
45 po::options_description description(
Davide Pesaventob310efb2019-04-11 22:10:24 -040046 "Usage: ndnsec cert-gen [-h] [-S TIMESTAMP] [-E TIMESTAMP] [-I INFO]...\n"
47 " [-s IDENTITY] [-i ISSUER] [-r] FILE\n"
48 "\n"
49 "Options");
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 description.add_options()
51 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040052 ("request,r", po::value<std::string>(&requestFile)->default_value("-"),
Davide Pesavento58de07a2022-06-08 00:02:02 -040053 "certificate request file name, '-' for stdin (the default)")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080054 ("not-before,S", po::value<std::string>(&notBeforeStr),
Davide Pesaventob310efb2019-04-11 22:10:24 -040055 "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080056 ("not-after,E", po::value<std::string>(&notAfterStr),
Davide Pesaventob310efb2019-04-11 22:10:24 -040057 "certificate validity end date/time in YYYYMMDDhhmmss format (default: "
58 "365 days after the --not-before timestamp)")
Alexander Afanasyev35109a12017-01-04 15:39:06 -080059 ("info,I", po::value<std::vector<std::string>>(&infos),
Davide Pesavento58de07a2022-06-08 00:02:02 -040060 "key and value (separated by a single space) of the additional description "
61 "to be included in the issued certificate (e.g., \"affiliation University of "
62 "California, Los Angeles\"); this option may be repeated multiple times")
Davide Pesaventob310efb2019-04-11 22:10:24 -040063 ("sign-id,s", po::value<Name>(&signId), "signing identity")
Davide Pesavento58de07a2022-06-08 00:02:02 -040064 ("issuer-id,i", po::value<std::string>(&issuer)->default_value(Certificate::DEFAULT_ISSUER_ID.toUri()),
65 "issuer's ID to be included in the issued certificate name, interpreted as a "
66 "name component in URI format")
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080067 ;
68
69 po::positional_options_description p;
70 p.add("request", 1);
71
72 po::variables_map vm;
73 try {
74 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
75 po::notify(vm);
76 }
77 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040078 std::cerr << "ERROR: " << e.what() << "\n\n"
79 << description << std::endl;
80 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080081 }
82
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 if (vm.count("help") > 0) {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 std::cout << description << std::endl;
85 return 0;
86 }
87
Davide Pesaventof2cae612021-03-24 18:47:05 -040088 security::AdditionalDescription additionalDescription;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080089
Alexander Afanasyev35109a12017-01-04 15:39:06 -080090 for (const auto& info : infos) {
Davide Pesaventof2cae612021-03-24 18:47:05 -040091 auto 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;
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080095 }
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
Davide Pesavento187e9f92023-03-20 22:46:22 -0400102 time::system_clock::time_point notBefore;
103 time::system_clock::time_point notAfter;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800104
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) {
Davide Pesavento0f830802018-01-16 23:58:58 -0500113 notAfter = notBefore + 365_days;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800114 }
115 else {
116 notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
117
118 if (notAfter < notBefore) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400119 std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl;
120 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800121 }
122 }
123
Davide Pesavento58de07a2022-06-08 00:02:02 -0400124 auto issuerId = name::Component::fromEscapedString(issuer);
125 if (issuerId.isImplicitSha256Digest() ||
126 issuerId.isParametersSha256Digest() ||
127 issuerId.isKeyword()) {
128 std::cerr << "ERROR: '" << issuerId << "' cannot be used as issuer ID" << std::endl;
129 return 2;
130 }
131
Davide Pesaventof2cae612021-03-24 18:47:05 -0400132 KeyChain keyChain;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400133
Davide Pesavento58de07a2022-06-08 00:02:02 -0400134 auto request = loadFromFile<Certificate>(requestFile);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800135
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000136 security::SigningInfo signer;
137 if (vm.count("sign-id") > 0) {
138 signer.setSigningIdentity(signId);
139 }
Alexander Afanasyev464da532017-09-02 12:16:32 -0400140 if (!additionalDescription.empty()) {
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000141 SignatureInfo sigInfo;
142 sigInfo.addCustomTlv(additionalDescription.wireEncode());
143 signer.setSignatureInfo(sigInfo);
Alexander Afanasyev464da532017-09-02 12:16:32 -0400144 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800145
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000146 security::MakeCertificateOptions opts;
Davide Pesavento58de07a2022-06-08 00:02:02 -0400147 opts.issuerId = issuerId;
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000148 opts.validity.emplace(notBefore, notAfter);
149 auto cert = keyChain.makeCertificate(request, signer, opts);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800150
Davide Pesaventob310efb2019-04-11 22:10:24 -0400151 {
152 using namespace security::transform;
Davide Pesavento258d51a2022-02-27 21:26:28 -0500153 bufferSource(cert.wireEncode()) >> base64Encode(true) >> streamSink(std::cout);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800154 }
155
156 return 0;
157}
158
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400159} // namespace ndn::ndnsec