Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_CERT_GEN_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_CERT_GEN_HPP |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 30 | ndnsec_cert_gen(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 31 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | using boost::tokenizer; |
| 33 | using boost::escaped_list_separator; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | using namespace ndn; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 36 | using namespace ndn::time; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 37 | using namespace ndn::security; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 38 | namespace po = boost::program_options; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 40 | KeyChain keyChain; |
| 41 | |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 42 | std::string notBeforeStr; |
| 43 | std::string notAfterStr; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 44 | std::string subjectName; |
| 45 | std::string requestFile("-"); |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 46 | Name signId; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 47 | std::string subjectInfo; |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 48 | std::vector<std::string> signedInfo; |
| 49 | Name certPrefix = KeyChain::DEFAULT_PREFIX; // to avoid displaying the default value |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 50 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 51 | po::options_description description( |
| 52 | "General Usage\n" |
| 53 | " ndnsec cert-gen [-h] [-S date] [-E date] [-N subject-name] [-I subject-info] " |
| 54 | "[-s sign-id] [-p cert-prefix] request\n" |
| 55 | "General options"); |
| 56 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 57 | description.add_options() |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 58 | ("help,h", "produce help message") |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 59 | ("not-before,S", po::value<std::string>(¬BeforeStr), |
| 60 | "certificate starting date, YYYYMMDDhhmmss (default: now)") |
| 61 | ("not-after,E", po::value<std::string>(¬AfterStr), |
| 62 | "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)") |
| 63 | ("subject-name,N", po::value<std::string>(&subjectName), |
| 64 | "subject name") |
| 65 | ("subject-info,I", po::value<std::string>(&subjectInfo), |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 66 | "(deprecated, uses 'signed-info') subject info, pairs of OID and string " |
| 67 | " description: \"2.5.4.10 'University of California, Los Angeles'\"") |
| 68 | ("signed-info", po::value<std::vector<std::string> >(&signedInfo), |
| 69 | "a pair of OID and string (must be separated by a single space), e.g., " |
| 70 | "\"2.5.4.10 University of California, Los Angeles\". " |
| 71 | "May be repeated multiple times") |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 72 | ("sign-id,s", po::value<Name>(&signId)->default_value(keyChain.getDefaultIdentity()), |
| 73 | "signing identity") |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 74 | ("cert-prefix,p", po::value<Name>(&certPrefix), |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 75 | "cert prefix, which is the part of certificate name before " |
| 76 | "KEY component") |
| 77 | ("request,r", po::value<std::string>(&requestFile)->default_value("-"), |
| 78 | "request file name, - for stdin") |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 79 | ; |
| 80 | |
| 81 | po::positional_options_description p; |
| 82 | p.add("request", 1); |
| 83 | |
| 84 | po::variables_map vm; |
| 85 | try |
| 86 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 88 | vm); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 89 | po::notify(vm); |
| 90 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 91 | catch (const std::exception& e) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 92 | { |
| 93 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 94 | return 1; |
| 95 | } |
| 96 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 97 | if (vm.count("help") != 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 98 | { |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 99 | std::cout << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 100 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 103 | if (vm.count("subject-name") == 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 104 | { |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 105 | std::cerr << "ERROR: subject name must be specified" << std::endl |
| 106 | << std::endl |
| 107 | << description << std::endl; |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 108 | return 1; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 111 | std::vector<v1::CertificateSubjectDescription> subjectDescription; |
| 112 | subjectDescription.push_back(v1::CertificateSubjectDescription(oid::ATTRIBUTE_NAME, subjectName)); |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 114 | // 'subjectInfo' is deprecated and the following block will be removed eventually |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 115 | tokenizer<escaped_list_separator<char> > subjectInfoItems |
Yingdi Yu | 9d9d599 | 2014-06-25 12:25:16 -0700 | [diff] [blame] | 116 | (subjectInfo, escaped_list_separator<char>("\\", " \t", "'\"")); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 117 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 118 | tokenizer<escaped_list_separator<char> >::iterator it = |
| 119 | subjectInfoItems.begin(); |
| 120 | |
| 121 | while (it != subjectInfoItems.end()) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 122 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 123 | std::string oid = *it; |
| 124 | |
| 125 | it++; |
| 126 | if (it == subjectInfoItems.end()) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 127 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 128 | std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl; |
| 129 | return 1; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 130 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 131 | |
| 132 | std::string value = *it; |
| 133 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 134 | subjectDescription.push_back(v1::CertificateSubjectDescription(Oid(oid), value)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 135 | |
| 136 | it++; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 139 | // new 'signedInfo' processing |
| 140 | for (std::vector<std::string>::const_iterator info = signedInfo.begin(); |
| 141 | info != signedInfo.end(); ++info) { |
| 142 | size_t pos = info->find(" "); |
| 143 | if (pos == std::string::npos) { |
| 144 | std::cerr << "ERROR: incorrectly formatted signed info block [" << *info << "]" << std::endl; |
| 145 | return 1; |
| 146 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 147 | Oid oid(info->substr(0, pos)); |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 148 | std::string value = info->substr(pos + 1); |
| 149 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 150 | subjectDescription.push_back(v1::CertificateSubjectDescription(oid, value)); |
Alexander Afanasyev | 66cd424 | 2014-10-18 22:39:49 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 153 | system_clock::TimePoint notBefore; |
| 154 | system_clock::TimePoint notAfter; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 155 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 156 | if (vm.count("not-before") == 0) |
| 157 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 158 | notBefore = system_clock::now(); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 159 | } |
| 160 | else |
| 161 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 162 | notBefore = fromIsoString(notBeforeStr.substr(0, 8) + "T" + |
| 163 | notBeforeStr.substr(8, 6)); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 164 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 165 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 166 | if (vm.count("not-after") == 0) |
| 167 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 168 | notAfter = notBefore + days(365); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 169 | } |
| 170 | else |
| 171 | { |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 172 | notAfter = fromIsoString(notAfterStr.substr(0, 8) + "T" + |
| 173 | notAfterStr.substr(8, 6)); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 174 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 175 | if (notAfter < notBefore) |
| 176 | { |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 177 | std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl |
| 178 | << std::endl |
| 179 | << description << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 180 | return 1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (vm.count("request") == 0) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 185 | { |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 186 | std::cerr << "ERROR: request file must be specified" << std::endl |
| 187 | << std::endl |
| 188 | << description << std::endl; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 189 | return 1; |
| 190 | } |
| 191 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 192 | shared_ptr<v1::IdentityCertificate> selfSignedCertificate |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 193 | = getIdentityCertificate(requestFile); |
| 194 | |
| 195 | if (!static_cast<bool>(selfSignedCertificate)) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 196 | { |
| 197 | std::cerr << "ERROR: input error" << std::endl; |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | Name keyName = selfSignedCertificate->getPublicKeyName(); |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 203 | shared_ptr<v1::IdentityCertificate> certificate = |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 204 | keyChain.prepareUnsignedIdentityCertificate(keyName, selfSignedCertificate->getPublicKeyInfo(), |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 205 | signId, notBefore, notAfter, |
| 206 | subjectDescription, certPrefix); |
Yingdi Yu | 05842f2 | 2014-04-15 19:21:56 -0700 | [diff] [blame] | 207 | |
Yingdi Yu | c4c8120 | 2014-07-08 11:07:50 -0700 | [diff] [blame] | 208 | if (!static_cast<bool>(certificate)) |
| 209 | { |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 210 | std::cerr << "ERROR: key name is not formated correctly or does not match certificate name" |
Yingdi Yu | c4c8120 | 2014-07-08 11:07:50 -0700 | [diff] [blame] | 211 | << std::endl; |
| 212 | return 1; |
| 213 | } |
| 214 | |
Yingdi Yu | ba8604d | 2014-10-13 19:03:12 -0700 | [diff] [blame] | 215 | keyChain.createIdentity(signId); |
| 216 | Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(signId); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 217 | keyChain.sign(*certificate, |
| 218 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT, |
| 219 | signingCertificateName)); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 220 | |
Yingdi Yu | 0eb5d72 | 2014-06-10 15:06:25 -0700 | [diff] [blame] | 221 | Block wire = certificate->wireEncode(); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 223 | try { |
| 224 | transform::bufferSource(wire.wire(), wire.size()) >> transform::base64Encode(true) >> transform::streamSink(std::cout); |
| 225 | } |
| 226 | catch (const transform::Error& e) { |
| 227 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 228 | return 1; |
| 229 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 233 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 234 | #endif // NDN_TOOLS_NDNSEC_CERT_GEN_HPP |