blob: 04da04014305362c590fe75254358e48565d6aaf [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yue6bfab22014-02-06 16:01:19 -080022 */
23
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_CERT_GEN_HPP
25#define NDN_TOOLS_NDNSEC_CERT_GEN_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yue6bfab22014-02-06 16:01:19 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030ndnsec_cert_gen(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080031{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 using boost::tokenizer;
33 using boost::escaped_list_separator;
Yingdi Yue6bfab22014-02-06 16:01:19 -080034
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 using namespace ndn;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070036 using namespace ndn::time;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070037 using namespace ndn::security;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080038 namespace po = boost::program_options;
Yingdi Yue6bfab22014-02-06 16:01:19 -080039
Alexander Afanasyev66cd4242014-10-18 22:39:49 -070040 KeyChain keyChain;
41
Yingdi Yue6bfab22014-02-06 16:01:19 -080042 std::string notBeforeStr;
43 std::string notAfterStr;
Yingdi Yub61f5402014-02-26 17:46:11 -080044 std::string subjectName;
45 std::string requestFile("-");
Yingdi Yuba8604d2014-10-13 19:03:12 -070046 Name signId;
Yingdi Yub61f5402014-02-26 17:46:11 -080047 std::string subjectInfo;
Alexander Afanasyev66cd4242014-10-18 22:39:49 -070048 std::vector<std::string> signedInfo;
49 Name certPrefix = KeyChain::DEFAULT_PREFIX; // to avoid displaying the default value
Yingdi Yue6bfab22014-02-06 16:01:19 -080050
Yingdi Yu0eb5d722014-06-10 15:06:25 -070051 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 Yub61f5402014-02-26 17:46:11 -080057 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080058 ("help,h", "produce help message")
Yingdi Yuba8604d2014-10-13 19:03:12 -070059 ("not-before,S", po::value<std::string>(&notBeforeStr),
60 "certificate starting date, YYYYMMDDhhmmss (default: now)")
61 ("not-after,E", po::value<std::string>(&notAfterStr),
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 Afanasyev66cd4242014-10-18 22:39:49 -070066 "(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 Yuba8604d2014-10-13 19:03:12 -070072 ("sign-id,s", po::value<Name>(&signId)->default_value(keyChain.getDefaultIdentity()),
73 "signing identity")
Alexander Afanasyev66cd4242014-10-18 22:39:49 -070074 ("cert-prefix,p", po::value<Name>(&certPrefix),
Yingdi Yuba8604d2014-10-13 19:03:12 -070075 "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 Yue6bfab22014-02-06 16:01:19 -080079 ;
80
81 po::positional_options_description p;
82 p.add("request", 1);
83
84 po::variables_map vm;
85 try
86 {
Yingdi Yub61f5402014-02-26 17:46:11 -080087 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
88 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -080089 po::notify(vm);
90 }
Yingdi Yub61f5402014-02-26 17:46:11 -080091 catch (const std::exception& e)
Yingdi Yue6bfab22014-02-06 16:01:19 -080092 {
93 std::cerr << "ERROR: " << e.what() << std::endl;
94 return 1;
95 }
96
Yingdi Yub61f5402014-02-26 17:46:11 -080097 if (vm.count("help") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -080098 {
Yingdi Yuba8604d2014-10-13 19:03:12 -070099 std::cout << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800100 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800101 }
102
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700103 if (vm.count("subject-name") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800104 {
Yingdi Yuba8604d2014-10-13 19:03:12 -0700105 std::cerr << "ERROR: subject name must be specified" << std::endl
106 << std::endl
107 << description << std::endl;
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700108 return 1;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800109 }
110
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700111 std::vector<v1::CertificateSubjectDescription> subjectDescription;
112 subjectDescription.push_back(v1::CertificateSubjectDescription(oid::ATTRIBUTE_NAME, subjectName));
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700113
Alexander Afanasyev66cd4242014-10-18 22:39:49 -0700114 // 'subjectInfo' is deprecated and the following block will be removed eventually
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 tokenizer<escaped_list_separator<char> > subjectInfoItems
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700116 (subjectInfo, escaped_list_separator<char>("\\", " \t", "'\""));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800117
Yingdi Yub61f5402014-02-26 17:46:11 -0800118 tokenizer<escaped_list_separator<char> >::iterator it =
119 subjectInfoItems.begin();
120
121 while (it != subjectInfoItems.end())
Yingdi Yue6bfab22014-02-06 16:01:19 -0800122 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800123 std::string oid = *it;
124
125 it++;
126 if (it == subjectInfoItems.end())
Yingdi Yue6bfab22014-02-06 16:01:19 -0800127 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800128 std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl;
129 return 1;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800130 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800131
132 std::string value = *it;
133
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700134 subjectDescription.push_back(v1::CertificateSubjectDescription(Oid(oid), value));
Yingdi Yub61f5402014-02-26 17:46:11 -0800135
136 it++;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800137 }
138
Alexander Afanasyev66cd4242014-10-18 22:39:49 -0700139 // 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 Afanasyev2fa59392016-07-29 17:24:23 -0700147 Oid oid(info->substr(0, pos));
Alexander Afanasyev66cd4242014-10-18 22:39:49 -0700148 std::string value = info->substr(pos + 1);
149
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700150 subjectDescription.push_back(v1::CertificateSubjectDescription(oid, value));
Alexander Afanasyev66cd4242014-10-18 22:39:49 -0700151 }
152
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700153 system_clock::TimePoint notBefore;
154 system_clock::TimePoint notAfter;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800155
Yingdi Yub61f5402014-02-26 17:46:11 -0800156 if (vm.count("not-before") == 0)
157 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700158 notBefore = system_clock::now();
Yingdi Yub61f5402014-02-26 17:46:11 -0800159 }
160 else
161 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700162 notBefore = fromIsoString(notBeforeStr.substr(0, 8) + "T" +
163 notBeforeStr.substr(8, 6));
Yingdi Yub61f5402014-02-26 17:46:11 -0800164 }
Yingdi Yue6bfab22014-02-06 16:01:19 -0800165
Yingdi Yub61f5402014-02-26 17:46:11 -0800166 if (vm.count("not-after") == 0)
167 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700168 notAfter = notBefore + days(365);
Yingdi Yub61f5402014-02-26 17:46:11 -0800169 }
170 else
171 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172 notAfter = fromIsoString(notAfterStr.substr(0, 8) + "T" +
173 notAfterStr.substr(8, 6));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800174
Yingdi Yub61f5402014-02-26 17:46:11 -0800175 if (notAfter < notBefore)
176 {
Yingdi Yuba8604d2014-10-13 19:03:12 -0700177 std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl
178 << std::endl
179 << description << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800180 return 1;
181 }
182 }
183
184 if (vm.count("request") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800185 {
Yingdi Yuba8604d2014-10-13 19:03:12 -0700186 std::cerr << "ERROR: request file must be specified" << std::endl
187 << std::endl
188 << description << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800189 return 1;
190 }
191
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700192 shared_ptr<v1::IdentityCertificate> selfSignedCertificate
Yingdi Yub61f5402014-02-26 17:46:11 -0800193 = getIdentityCertificate(requestFile);
194
195 if (!static_cast<bool>(selfSignedCertificate))
Yingdi Yue6bfab22014-02-06 16:01:19 -0800196 {
197 std::cerr << "ERROR: input error" << std::endl;
198 return 1;
199 }
200
201 Name keyName = selfSignedCertificate->getPublicKeyName();
Yingdi Yu05842f22014-04-15 19:21:56 -0700202
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700203 shared_ptr<v1::IdentityCertificate> certificate =
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700204 keyChain.prepareUnsignedIdentityCertificate(keyName, selfSignedCertificate->getPublicKeyInfo(),
Yingdi Yuba8604d2014-10-13 19:03:12 -0700205 signId, notBefore, notAfter,
206 subjectDescription, certPrefix);
Yingdi Yu05842f22014-04-15 19:21:56 -0700207
Yingdi Yuc4c81202014-07-08 11:07:50 -0700208 if (!static_cast<bool>(certificate))
209 {
Yingdi Yuba8604d2014-10-13 19:03:12 -0700210 std::cerr << "ERROR: key name is not formated correctly or does not match certificate name"
Yingdi Yuc4c81202014-07-08 11:07:50 -0700211 << std::endl;
212 return 1;
213 }
214
Yingdi Yuba8604d2014-10-13 19:03:12 -0700215 keyChain.createIdentity(signId);
216 Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(signId);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700217 keyChain.sign(*certificate,
218 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT,
219 signingCertificateName));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800220
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700221 Block wire = certificate->wireEncode();
Yingdi Yue6bfab22014-02-06 16:01:19 -0800222
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700223 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 Yue6bfab22014-02-06 16:01:19 -0800230
231 return 0;
232}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800233
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800234#endif // NDN_TOOLS_NDNSEC_CERT_GEN_HPP