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