| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2013-2020 Regents of the University of California. |
| * |
| * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| * |
| * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| * terms of the GNU Lesser General Public License as published by the Free Software |
| * Foundation, either version 3 of the License, or (at your option) any later version. |
| * |
| * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License and GNU Lesser |
| * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| * <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| */ |
| |
| #include "ndnsec.hpp" |
| #include "util.hpp" |
| |
| #include "ndn-cxx/security/transform/base64-encode.hpp" |
| #include "ndn-cxx/security/transform/buffer-source.hpp" |
| #include "ndn-cxx/security/transform/public-key.hpp" |
| #include "ndn-cxx/security/transform/stream-sink.hpp" |
| #include "ndn-cxx/security/v2/additional-description.hpp" |
| |
| namespace ndn { |
| namespace ndnsec { |
| |
| int |
| ndnsec_cert_gen(int argc, char** argv) |
| { |
| namespace po = boost::program_options; |
| |
| std::string requestFile; |
| std::string notBeforeStr; |
| std::string notAfterStr; |
| std::vector<std::string> infos; |
| Name signId; |
| std::string issuerId; |
| |
| po::options_description description( |
| "Usage: ndnsec cert-gen [-h] [-S TIMESTAMP] [-E TIMESTAMP] [-I INFO]...\n" |
| " [-s IDENTITY] [-i ISSUER] [-r] FILE\n" |
| "\n" |
| "Options"); |
| description.add_options() |
| ("help,h", "produce help message") |
| ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| "request file name, '-' for stdin (the default)") |
| ("not-before,S", po::value<std::string>(¬BeforeStr), |
| "certificate validity start date/time in YYYYMMDDhhmmss format (default: now)") |
| ("not-after,E", po::value<std::string>(¬AfterStr), |
| "certificate validity end date/time in YYYYMMDDhhmmss format (default: " |
| "365 days after the --not-before timestamp)") |
| ("info,I", po::value<std::vector<std::string>>(&infos), |
| "key and value (must be separated by a single space) of the additional " |
| "description to be included in the issued certificate (e.g., " |
| "\"affiliation University of California, Los Angeles\"); " |
| "this option may be repeated multiple times") |
| ("sign-id,s", po::value<Name>(&signId), "signing identity") |
| ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"), |
| "issuer's ID to be included in the issued certificate name") |
| ; |
| |
| po::positional_options_description p; |
| p.add("request", 1); |
| |
| po::variables_map vm; |
| try { |
| po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| po::notify(vm); |
| } |
| catch (const std::exception& e) { |
| std::cerr << "ERROR: " << e.what() << "\n\n" |
| << description << std::endl; |
| return 2; |
| } |
| |
| if (vm.count("help") > 0) { |
| std::cout << description << std::endl; |
| return 0; |
| } |
| |
| security::v2::AdditionalDescription additionalDescription; |
| |
| for (const auto& info : infos) { |
| auto pos = info.find(" "); |
| if (pos == std::string::npos) { |
| std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl; |
| return 2; |
| } |
| std::string key = info.substr(0, pos); |
| std::string value = info.substr(pos + 1); |
| |
| additionalDescription.set(key, value); |
| } |
| |
| time::system_clock::TimePoint notBefore; |
| time::system_clock::TimePoint notAfter; |
| |
| if (vm.count("not-before") == 0) { |
| notBefore = time::system_clock::now(); |
| } |
| else { |
| notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6)); |
| } |
| |
| if (vm.count("not-after") == 0) { |
| notAfter = notBefore + 365_days; |
| } |
| else { |
| notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| |
| if (notAfter < notBefore) { |
| std::cerr << "ERROR: '--not-before' cannot be later than '--not-after'" << std::endl; |
| return 2; |
| } |
| } |
| |
| security::v2::KeyChain keyChain; |
| |
| security::v2::Certificate certRequest; |
| try { |
| certRequest = loadCertificate(requestFile); |
| } |
| catch (const CannotLoadCertificate&) { |
| std::cerr << "ERROR: Cannot load the request from `" << requestFile << "`" << std::endl; |
| return 1; |
| } |
| |
| // validate that the content is a public key |
| Buffer keyContent = certRequest.getPublicKey(); |
| security::transform::PublicKey pubKey; |
| pubKey.loadPkcs8(keyContent.data(), keyContent.size()); |
| |
| Name certName = certRequest.getKeyName(); |
| certName |
| .append(issuerId) |
| .appendVersion(); |
| |
| security::v2::Certificate cert; |
| cert.setName(certName); |
| cert.setContent(certRequest.getContent()); |
| // TODO: add ability to customize |
| cert.setFreshnessPeriod(1_h); |
| |
| SignatureInfo signatureInfo; |
| signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
| if (!additionalDescription.empty()) { |
| signatureInfo.addCustomTlv(additionalDescription.wireEncode()); |
| } |
| |
| security::Identity identity; |
| if (vm.count("sign-id") == 0) { |
| identity = keyChain.getPib().getDefaultIdentity(); |
| } |
| else { |
| identity = keyChain.getPib().getIdentity(signId); |
| } |
| |
| keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
| |
| const Block& wire = cert.wireEncode(); |
| { |
| using namespace security::transform; |
| bufferSource(wire.wire(), wire.size()) >> base64Encode(true) >> streamSink(std::cout); |
| } |
| |
| return 0; |
| } |
| |
| } // namespace ndnsec |
| } // namespace ndn |