Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/security/additional-description.hpp" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 26 | #include "ndn-cxx/security/transform/base64-encode.hpp" |
| 27 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 28 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | |
| 32 | int |
| 33 | ndnsec_cert_gen(int argc, char** argv) |
| 34 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 35 | namespace po = boost::program_options; |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 36 | using security::Certificate; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 37 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 38 | std::string requestFile; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 39 | std::string notBeforeStr; |
| 40 | std::string notAfterStr; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 41 | std::vector<std::string> infos; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 42 | Name signId; |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 43 | std::string issuer; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 44 | |
| 45 | po::options_description description( |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 46 | "Usage: ndnsec cert-gen [-h] [-S TIMESTAMP] [-E TIMESTAMP] [-I INFO]...\n" |
| 47 | " [-s IDENTITY] [-i ISSUER] [-r] FILE\n" |
| 48 | "\n" |
Davide Pesavento | 25d9c9f | 2024-06-23 15:10:05 -0400 | [diff] [blame^] | 49 | "Issue a certificate from a certificate signing request.\n" |
| 50 | "\n" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 51 | "Options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 52 | description.add_options() |
| 53 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 54 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 55 | "certificate request file name, '-' for stdin (the default)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 56 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 57 | "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 58 | ("not-after,E", po::value<std::string>(¬AfterStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 59 | "certificate validity end date/time in YYYYMMDDhhmmss format (default: " |
| 60 | "365 days after the --not-before timestamp)") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 61 | ("info,I", po::value<std::vector<std::string>>(&infos), |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 62 | "key and value (separated by a single space) of the additional description " |
| 63 | "to be included in the issued certificate (e.g., \"affiliation University of " |
| 64 | "California, Los Angeles\"); this option may be repeated multiple times") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 65 | ("sign-id,s", po::value<Name>(&signId), "signing identity") |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 66 | ("issuer-id,i", po::value<std::string>(&issuer)->default_value(Certificate::DEFAULT_ISSUER_ID.toUri()), |
| 67 | "issuer's ID to be included in the issued certificate name, interpreted as a " |
| 68 | "name component in URI format") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 69 | ; |
| 70 | |
| 71 | po::positional_options_description p; |
| 72 | p.add("request", 1); |
| 73 | |
| 74 | po::variables_map vm; |
| 75 | try { |
| 76 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 77 | po::notify(vm); |
| 78 | } |
| 79 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 80 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 81 | << description << std::endl; |
| 82 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 85 | if (vm.count("help") > 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 86 | std::cout << description << std::endl; |
| 87 | return 0; |
| 88 | } |
| 89 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 90 | security::AdditionalDescription additionalDescription; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 92 | for (const auto& info : infos) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 93 | auto pos = info.find(' '); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | if (pos == std::string::npos) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 95 | std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 96 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 97 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 98 | std::string key = info.substr(0, pos); |
| 99 | std::string value = info.substr(pos + 1); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 100 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 101 | additionalDescription.set(key, value); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Davide Pesavento | 187e9f9 | 2023-03-20 22:46:22 -0400 | [diff] [blame] | 104 | time::system_clock::time_point notBefore; |
| 105 | time::system_clock::time_point notAfter; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 115 | notAfter = notBefore + 365_days; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 116 | } |
| 117 | else { |
| 118 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| 119 | |
| 120 | if (notAfter < notBefore) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 121 | std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl; |
| 122 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 126 | auto issuerId = name::Component::fromUri(issuer); |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 127 | if (issuerId.isImplicitSha256Digest() || |
| 128 | issuerId.isParametersSha256Digest() || |
| 129 | issuerId.isKeyword()) { |
| 130 | std::cerr << "ERROR: '" << issuerId << "' cannot be used as issuer ID" << std::endl; |
| 131 | return 2; |
| 132 | } |
| 133 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 134 | KeyChain keyChain; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 135 | |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 136 | auto request = loadFromFile<Certificate>(requestFile); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 137 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 138 | security::SigningInfo signer; |
| 139 | if (vm.count("sign-id") > 0) { |
| 140 | signer.setSigningIdentity(signId); |
| 141 | } |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 142 | if (!additionalDescription.empty()) { |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 143 | SignatureInfo sigInfo; |
| 144 | sigInfo.addCustomTlv(additionalDescription.wireEncode()); |
| 145 | signer.setSignatureInfo(sigInfo); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 146 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 147 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 148 | security::MakeCertificateOptions opts; |
Davide Pesavento | 58de07a | 2022-06-08 00:02:02 -0400 | [diff] [blame] | 149 | opts.issuerId = issuerId; |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 150 | opts.validity.emplace(notBefore, notAfter); |
| 151 | auto cert = keyChain.makeCertificate(request, signer, opts); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 152 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 153 | { |
| 154 | using namespace security::transform; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 155 | bufferSource(cert.wireEncode()) >> base64Encode(true) >> streamSink(std::cout); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 161 | } // namespace ndn::ndnsec |