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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 25 | #include <boost/tokenizer.hpp> |
| 26 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace ndnsec { |
| 29 | |
| 30 | int |
| 31 | ndnsec_cert_gen(int argc, char** argv) |
| 32 | { |
| 33 | using boost::tokenizer; |
| 34 | using boost::escaped_list_separator; |
| 35 | |
| 36 | namespace po = boost::program_options; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 37 | namespace t = security::transform; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 39 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 40 | |
| 41 | std::string notBeforeStr; |
| 42 | std::string notAfterStr; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 43 | std::string requestFile("-"); |
| 44 | Name signId; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 45 | std::vector<std::string> infos; |
| 46 | std::string issuerId; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 47 | |
| 48 | po::options_description description( |
| 49 | "General Usage\n" |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 50 | " ndnsec cert-gen [-h] [-S date] [-E date] [-I info] [-s sign-id] request\n" |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 51 | "General options"); |
| 52 | |
| 53 | description.add_options() |
| 54 | ("help,h", "produce help message") |
| 55 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
| 56 | "certificate starting date, YYYYMMDDhhmmss (default: now)") |
| 57 | ("not-after,E", po::value<std::string>(¬AfterStr), |
| 58 | "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)") |
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 " |
| 61 | "description to be included in the issued certificate, e.g., " |
| 62 | "\"affiliation University of California, Los Angeles\". " |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 63 | "May be repeated multiple times") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 64 | ("sign-id,s", po::value<Name>(&signId), |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 65 | "signing identity") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 66 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| 67 | "request file name, - for stdin") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 68 | ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"), |
| 69 | "issuer's ID to be included as part of the issued certificate name") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 70 | ; |
| 71 | |
| 72 | po::positional_options_description p; |
| 73 | p.add("request", 1); |
| 74 | |
| 75 | po::variables_map vm; |
| 76 | try { |
| 77 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 78 | po::notify(vm); |
| 79 | } |
| 80 | catch (const std::exception& e) { |
| 81 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | if (vm.count("help") != 0) { |
| 86 | std::cout << description << std::endl; |
| 87 | return 0; |
| 88 | } |
| 89 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 90 | security::v2::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) { |
| 93 | size_t 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; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 96 | return 1; |
| 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 | |
| 104 | time::system_clock::TimePoint notBefore; |
| 105 | time::system_clock::TimePoint notAfter; |
| 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) { |
| 121 | std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl |
| 122 | << std::endl |
| 123 | << description << std::endl; |
| 124 | return 1; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (vm.count("request") == 0) { |
| 129 | std::cerr << "ERROR: request file must be specified" << std::endl |
| 130 | << std::endl |
| 131 | << description << std::endl; |
| 132 | return 1; |
| 133 | } |
| 134 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 135 | security::v2::Certificate certRequest = loadCertificate(requestFile); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 136 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 137 | // validate that the content is a public key |
| 138 | try { |
| 139 | Buffer keyContent = certRequest.getPublicKey(); |
| 140 | t::PublicKey pubKey; |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 141 | pubKey.loadPkcs8(keyContent.data(), keyContent.size()); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 142 | } |
| 143 | catch (const std::exception& e) { |
| 144 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 145 | return 1; |
| 146 | } |
| 147 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 148 | security::v2::Certificate cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 150 | Name certName = certRequest.getKeyName(); |
| 151 | certName |
| 152 | .append(issuerId) |
| 153 | .appendVersion(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 154 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 155 | cert.setName(certName); |
| 156 | cert.setContent(certRequest.getContent()); |
| 157 | |
| 158 | // @TODO add ability to customize |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 159 | cert.setFreshnessPeriod(1_h); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 160 | |
| 161 | SignatureInfo signatureInfo; |
| 162 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame] | 163 | if (!additionalDescription.empty()) { |
| 164 | signatureInfo.appendTypeSpecificTlv(additionalDescription.wireEncode()); |
| 165 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 166 | |
| 167 | security::Identity identity; |
| 168 | if (vm.count("sign-id") == 0) { |
| 169 | identity = keyChain.getPib().getDefaultIdentity(); |
| 170 | } |
| 171 | else { |
| 172 | identity = keyChain.getPib().getIdentity(signId); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 175 | keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 177 | Block wire = cert.wireEncode(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 179 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 180 | try { |
| 181 | t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout); |
| 182 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 183 | catch (const t::Error& e) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 184 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | } // namespace ndnsec |
| 192 | } // namespace ndn |