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 | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2019 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 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 25 | #include "ndn-cxx/security/transform/base64-encode.hpp" |
| 26 | #include "ndn-cxx/security/transform/buffer-source.hpp" |
| 27 | #include "ndn-cxx/security/transform/public-key.hpp" |
| 28 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 29 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndnsec { |
| 32 | |
| 33 | int |
| 34 | ndnsec_cert_gen(int argc, char** argv) |
| 35 | { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 36 | namespace po = boost::program_options; |
| 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; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 43 | std::string issuerId; |
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" |
| 49 | "Options"); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 50 | description.add_options() |
| 51 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 52 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| 53 | "request file name, '-' for stdin (the default)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 54 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 55 | "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 56 | ("not-after,E", po::value<std::string>(¬AfterStr), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 57 | "certificate validity end date/time in YYYYMMDDhhmmss format (default: " |
| 58 | "365 days after the --not-before timestamp)") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 59 | ("info,I", po::value<std::vector<std::string>>(&infos), |
| 60 | "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^] | 61 | "description to be included in the issued certificate (e.g., " |
| 62 | "\"affiliation University of California, Los Angeles\"); " |
| 63 | "this option may be repeated multiple times") |
| 64 | ("sign-id,s", po::value<Name>(&signId), "signing identity") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 65 | ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"), |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 66 | "issuer's ID to be included in the issued certificate name") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 67 | ; |
| 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 Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 78 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 79 | << description << std::endl; |
| 80 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 83 | if (vm.count("help") > 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 84 | std::cout << description << std::endl; |
| 85 | return 0; |
| 86 | } |
| 87 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 88 | security::v2::AdditionalDescription additionalDescription; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 90 | for (const auto& info : infos) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 91 | auto pos = info.find(" "); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 92 | if (pos == std::string::npos) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 93 | std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 94 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 95 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 96 | std::string key = info.substr(0, pos); |
| 97 | std::string value = info.substr(pos + 1); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 99 | additionalDescription.set(key, value); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | time::system_clock::TimePoint notBefore; |
| 103 | time::system_clock::TimePoint notAfter; |
| 104 | |
| 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 113 | notAfter = notBefore + 365_days; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 114 | } |
| 115 | else { |
| 116 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| 117 | |
| 118 | if (notAfter < notBefore) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 119 | std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl; |
| 120 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 124 | security::v2::KeyChain keyChain; |
| 125 | |
| 126 | security::v2::Certificate certRequest; |
| 127 | try { |
| 128 | certRequest = loadCertificate(requestFile); |
| 129 | } |
| 130 | catch (const CannotLoadCertificate&) { |
| 131 | std::cerr << "ERROR: Cannot load the request from `" << requestFile << "`" << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 135 | // validate that the content is a public key |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 136 | Buffer keyContent = certRequest.getPublicKey(); |
| 137 | security::transform::PublicKey pubKey; |
| 138 | pubKey.loadPkcs8(keyContent.data(), keyContent.size()); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 140 | Name certName = certRequest.getKeyName(); |
| 141 | certName |
| 142 | .append(issuerId) |
| 143 | .appendVersion(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 144 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 145 | security::v2::Certificate cert; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 146 | cert.setName(certName); |
| 147 | cert.setContent(certRequest.getContent()); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 148 | // TODO: add ability to customize |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 149 | cert.setFreshnessPeriod(1_h); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 150 | |
| 151 | SignatureInfo signatureInfo; |
| 152 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 153 | if (!additionalDescription.empty()) { |
| 154 | signatureInfo.appendTypeSpecificTlv(additionalDescription.wireEncode()); |
| 155 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 156 | |
| 157 | security::Identity identity; |
| 158 | if (vm.count("sign-id") == 0) { |
| 159 | identity = keyChain.getPib().getDefaultIdentity(); |
| 160 | } |
| 161 | else { |
| 162 | identity = keyChain.getPib().getIdentity(signId); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 165 | keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 166 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame^] | 167 | const Block& wire = cert.wireEncode(); |
| 168 | { |
| 169 | using namespace security::transform; |
| 170 | bufferSource(wire.wire(), wire.size()) >> base64Encode(true) >> streamSink(std::cout); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | } // namespace ndnsec |
| 177 | } // namespace ndn |