Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 | |
| 88 | if (vm.count("subject-name") == 0) { |
| 89 | std::cerr << "ERROR: subject name must be specified" << std::endl |
| 90 | << std::endl |
| 91 | << description << std::endl; |
| 92 | return 1; |
| 93 | } |
| 94 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 95 | security::v2::AdditionalDescription additionalDescription; |
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 | for (const auto& info : infos) { |
| 98 | size_t pos = info.find(" "); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 99 | if (pos == std::string::npos) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 100 | std::cerr << "ERROR: incorrectly formatted info block [" << info << "]" << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 101 | return 1; |
| 102 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 103 | std::string key = info.substr(0, pos); |
| 104 | std::string value = info.substr(pos + 1); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 106 | additionalDescription.set(key, value); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | time::system_clock::TimePoint notBefore; |
| 110 | time::system_clock::TimePoint notAfter; |
| 111 | |
| 112 | if (vm.count("not-before") == 0) { |
| 113 | notBefore = time::system_clock::now(); |
| 114 | } |
| 115 | else { |
| 116 | notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6)); |
| 117 | } |
| 118 | |
| 119 | if (vm.count("not-after") == 0) { |
| 120 | notAfter = notBefore + time::days(365); |
| 121 | } |
| 122 | else { |
| 123 | notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6)); |
| 124 | |
| 125 | if (notAfter < notBefore) { |
| 126 | std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl |
| 127 | << std::endl |
| 128 | << description << std::endl; |
| 129 | return 1; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (vm.count("request") == 0) { |
| 134 | std::cerr << "ERROR: request file must be specified" << std::endl |
| 135 | << std::endl |
| 136 | << description << std::endl; |
| 137 | return 1; |
| 138 | } |
| 139 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 140 | security::v2::Certificate certRequest = loadCertificate(requestFile); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 142 | // validate that the content is a public key |
| 143 | try { |
| 144 | Buffer keyContent = certRequest.getPublicKey(); |
| 145 | t::PublicKey pubKey; |
| 146 | pubKey.loadPkcs8(keyContent.buf(), keyContent.size()); |
| 147 | } |
| 148 | catch (const std::exception& e) { |
| 149 | std::cerr << "ERROR: " << e.what() << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 150 | return 1; |
| 151 | } |
| 152 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 153 | security::v2::Certificate cert; |
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 | Name certName = certRequest.getKeyName(); |
| 156 | certName |
| 157 | .append(issuerId) |
| 158 | .appendVersion(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 160 | cert.setName(certName); |
| 161 | cert.setContent(certRequest.getContent()); |
| 162 | |
| 163 | // @TODO add ability to customize |
| 164 | cert.setFreshnessPeriod(time::hours(1)); |
| 165 | |
| 166 | SignatureInfo signatureInfo; |
| 167 | signatureInfo.setValidityPeriod(security::ValidityPeriod(notBefore, notAfter)); |
| 168 | |
| 169 | security::Identity identity; |
| 170 | if (vm.count("sign-id") == 0) { |
| 171 | identity = keyChain.getPib().getDefaultIdentity(); |
| 172 | } |
| 173 | else { |
| 174 | identity = keyChain.getPib().getIdentity(signId); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 177 | keyChain.sign(cert, security::SigningInfo(identity).setSignatureInfo(signatureInfo)); |
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 | Block wire = cert.wireEncode(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 182 | try { |
| 183 | t::bufferSource(wire.wire(), wire.size()) >> t::base64Encode(true) >> t::streamSink(std::cout); |
| 184 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 185 | catch (const t::Error& e) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 186 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | } // namespace ndnsec |
| 194 | } // namespace ndn |