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 | /* |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 25 | namespace ndn { |
| 26 | namespace ndnsec { |
| 27 | |
| 28 | int |
| 29 | ndnsec_cert_gen(int argc, char** argv) |
| 30 | { |
| 31 | using boost::tokenizer; |
| 32 | using boost::escaped_list_separator; |
| 33 | |
| 34 | namespace po = boost::program_options; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 35 | namespace t = security::transform; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 37 | security::v2::KeyChain keyChain; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | |
| 39 | std::string notBeforeStr; |
| 40 | std::string notAfterStr; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 41 | std::string requestFile("-"); |
| 42 | Name signId; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 43 | std::vector<std::string> infos; |
| 44 | std::string issuerId; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 45 | |
| 46 | po::options_description description( |
| 47 | "General Usage\n" |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 48 | " 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] | 49 | "General options"); |
| 50 | |
| 51 | description.add_options() |
| 52 | ("help,h", "produce help message") |
| 53 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
| 54 | "certificate starting date, YYYYMMDDhhmmss (default: now)") |
| 55 | ("not-after,E", po::value<std::string>(¬AfterStr), |
| 56 | "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 57 | ("info,I", po::value<std::vector<std::string>>(&infos), |
| 58 | "key and value (must be separated by a single space) of the additional " |
| 59 | "description to be included in the issued certificate, e.g., " |
| 60 | "\"affiliation University of California, Los Angeles\". " |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 61 | "May be repeated multiple times") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 62 | ("sign-id,s", po::value<Name>(&signId), |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 63 | "signing identity") |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 64 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| 65 | "request file name, - for stdin") |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 66 | ("issuer-id,i", po::value<std::string>(&issuerId)->default_value("NA"), |
| 67 | "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] | 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) { |
| 79 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | if (vm.count("help") != 0) { |
| 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) { |
| 91 | size_t 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; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 94 | return 1; |
| 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) { |
| 113 | notAfter = notBefore + time::days(365); |
| 114 | } |
| 115 | else { |
| 116 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| 117 | |
| 118 | if (notAfter < notBefore) { |
| 119 | std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl |
| 120 | << std::endl |
| 121 | << description << std::endl; |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (vm.count("request") == 0) { |
| 127 | std::cerr << "ERROR: request file must be specified" << std::endl |
| 128 | << std::endl |
| 129 | << description << std::endl; |
| 130 | return 1; |
| 131 | } |
| 132 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 133 | security::v2::Certificate certRequest = loadCertificate(requestFile); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 135 | // validate that the content is a public key |
| 136 | try { |
| 137 | Buffer keyContent = certRequest.getPublicKey(); |
| 138 | t::PublicKey pubKey; |
| 139 | pubKey.loadPkcs8(keyContent.buf(), keyContent.size()); |
| 140 | } |
| 141 | catch (const std::exception& e) { |
| 142 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 146 | security::v2::Certificate cert; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 148 | Name certName = certRequest.getKeyName(); |
| 149 | certName |
| 150 | .append(issuerId) |
| 151 | .appendVersion(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 152 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 153 | cert.setName(certName); |
| 154 | cert.setContent(certRequest.getContent()); |
| 155 | |
| 156 | // @TODO add ability to customize |
| 157 | cert.setFreshnessPeriod(time::hours(1)); |
| 158 | |
| 159 | SignatureInfo signatureInfo; |
| 160 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
Alexander Afanasyev | 464da53 | 2017-09-02 12:16:32 -0400 | [diff] [blame^] | 161 | if (!additionalDescription.empty()) { |
| 162 | signatureInfo.appendTypeSpecificTlv(additionalDescription.wireEncode()); |
| 163 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 164 | |
| 165 | security::Identity identity; |
| 166 | if (vm.count("sign-id") == 0) { |
| 167 | identity = keyChain.getPib().getDefaultIdentity(); |
| 168 | } |
| 169 | else { |
| 170 | identity = keyChain.getPib().getIdentity(signId); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 173 | keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 175 | Block wire = cert.wireEncode(); |
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 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 178 | try { |
| 179 | t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout); |
| 180 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 181 | catch (const t::Error& e) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 182 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | } // namespace ndnsec |
| 190 | } // namespace ndn |