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 | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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" |
| 28 | #include "ndn-cxx/security/transform/public-key.hpp" |
| 29 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 30 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace ndnsec { |
| 33 | |
| 34 | int |
| 35 | ndnsec_cert_gen(int argc, char** argv) |
| 36 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 37 | namespace po = boost::program_options; |
| 38 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 39 | std::string requestFile; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | std::string notBeforeStr; |
| 41 | std::string notAfterStr; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 42 | std::vector<std::string> infos; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 43 | Name signId; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 44 | std::string issuerId; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 45 | |
| 46 | po::options_description description( |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 47 | "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 Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 51 | description.add_options() |
| 52 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 53 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| 54 | "request file name, '-' for stdin (the default)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 55 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 56 | "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 57 | ("not-after,E", po::value<std::string>(¬AfterStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 58 | "certificate validity end date/time in YYYYMMDDhhmmss format (default: " |
| 59 | "365 days after the --not-before timestamp)") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 60 | ("info,I", po::value<std::vector<std::string>>(&infos), |
| 61 | "key and value (must be separated by a single space) of the additional " |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 62 | "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") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 66 | ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 67 | "issuer's ID to be included in the issued certificate name") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 68 | ; |
| 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) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 79 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 80 | << description << std::endl; |
| 81 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 84 | if (vm.count("help") > 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 85 | std::cout << description << std::endl; |
| 86 | return 0; |
| 87 | } |
| 88 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 89 | security::AdditionalDescription additionalDescription; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 91 | for (const auto& info : infos) { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 92 | auto pos = info.find(' '); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 93 | if (pos == std::string::npos) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 94 | std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 95 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 96 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 97 | std::string key = info.substr(0, pos); |
| 98 | std::string value = info.substr(pos + 1); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 100 | additionalDescription.set(key, value); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | time::system_clock::TimePoint notBefore; |
| 104 | time::system_clock::TimePoint notAfter; |
| 105 | |
| 106 | if (vm.count("not-before") == 0) { |
| 107 | notBefore = time::system_clock::now(); |
| 108 | } |
| 109 | else { |
| 110 | notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6)); |
| 111 | } |
| 112 | |
| 113 | if (vm.count("not-after") == 0) { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 114 | notAfter = notBefore + 365_days; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 115 | } |
| 116 | else { |
| 117 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| 118 | |
| 119 | if (notAfter < notBefore) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 120 | std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl; |
| 121 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 125 | KeyChain keyChain; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 126 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 127 | auto certRequest = loadFromFile<security::Certificate>(requestFile); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 129 | // validate that the content is a public key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 130 | auto keyContent = certRequest.getPublicKey(); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 131 | security::transform::PublicKey pubKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 132 | pubKey.loadPkcs8(keyContent); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 134 | Name certName = certRequest.getKeyName(); |
| 135 | certName |
| 136 | .append(issuerId) |
| 137 | .appendVersion(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 138 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 139 | security::Certificate cert; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 140 | cert.setName(certName); |
| 141 | cert.setContent(certRequest.getContent()); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 142 | // TODO: add ability to customize |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 143 | cert.setFreshnessPeriod(1_h); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 144 | |
| 145 | SignatureInfo signatureInfo; |
| 146 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 147 | if (!additionalDescription.empty()) { |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 148 | signatureInfo.addCustomTlv(additionalDescription.wireEncode()); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 149 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 150 | |
| 151 | security::Identity identity; |
| 152 | if (vm.count("sign-id") == 0) { |
| 153 | identity = keyChain.getPib().getDefaultIdentity(); |
| 154 | } |
| 155 | else { |
| 156 | identity = keyChain.getPib().getIdentity(signId); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 159 | keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 160 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 161 | { |
| 162 | using namespace security::transform; |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 163 | bufferSource(cert.wireEncode()) >> base64Encode(true) >> streamSink(std::cout); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | } // namespace ndnsec |
| 170 | } // namespace ndn |