blob: 71b2035224c7fe95112f0d69ddc19c1e02eb3f5d [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 Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024#ifndef NDNSEC_CERT_GEN_HPP
25#define NDNSEC_CERT_GEN_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080026
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027#include "ndnsec-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;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080037 namespace po = boost::program_options;
Yingdi Yue6bfab22014-02-06 16:01:19 -080038
Yingdi Yue6bfab22014-02-06 16:01:19 -080039 std::string notBeforeStr;
40 std::string notAfterStr;
Yingdi Yub61f5402014-02-26 17:46:11 -080041 std::string subjectName;
42 std::string requestFile("-");
Yingdi Yue6bfab22014-02-06 16:01:19 -080043 std::string signId;
Yingdi Yub61f5402014-02-26 17:46:11 -080044 std::string subjectInfo;
Yingdi Yu0eb5d722014-06-10 15:06:25 -070045 std::string certPrefix;
Yingdi Yu05842f22014-04-15 19:21:56 -070046 bool hasSignId = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -080047
Yingdi Yu0eb5d722014-06-10 15:06:25 -070048 po::options_description description(
49 "General Usage\n"
50 " ndnsec cert-gen [-h] [-S date] [-E date] [-N subject-name] [-I subject-info] "
51 "[-s sign-id] [-p cert-prefix] request\n"
52 "General options");
53
Yingdi Yub61f5402014-02-26 17:46:11 -080054 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080055 ("help,h", "produce help message")
Yingdi Yu0eb5d722014-06-10 15:06:25 -070056 ("not-before,S", po::value<std::string>(&notBeforeStr),
57 "certificate starting date, YYYYMMDDhhmmss")
58 ("not-after,E", po::value<std::string>(&notAfterStr),
59 "certificate ending date, YYYYMMDDhhmmss")
60 ("subject-name,N", po::value<std::string>(&subjectName),
61 "subject name")
62 ("subject-info,I", po::value<std::string>(&subjectInfo),
63 "subject info, pairs of OID and string description: "
64 "\"2.5.4.10 'University of California, Los Angeles'\"")
65 ("sign-id,s", po::value<std::string>(&signId),
66 "signing Identity, system default identity if not specified")
67 ("cert-prefix,p", po::value<std::string>(&certPrefix),
68 "cert prefix, which is the part of certificate name before "
69 "KEY component")
70 ("request,r", po::value<std::string>(&requestFile),
71 "request file name, - for stdin")
Yingdi Yue6bfab22014-02-06 16:01:19 -080072 ;
73
74 po::positional_options_description p;
75 p.add("request", 1);
76
77 po::variables_map vm;
78 try
79 {
Yingdi Yub61f5402014-02-26 17:46:11 -080080 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
81 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -080082 po::notify(vm);
83 }
Yingdi Yub61f5402014-02-26 17:46:11 -080084 catch (const std::exception& e)
Yingdi Yue6bfab22014-02-06 16:01:19 -080085 {
86 std::cerr << "ERROR: " << e.what() << std::endl;
87 return 1;
88 }
89
Yingdi Yub61f5402014-02-26 17:46:11 -080090 if (vm.count("help") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -080091 {
Yingdi Yub61f5402014-02-26 17:46:11 -080092 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -080094 }
95
Yingdi Yu05842f22014-04-15 19:21:56 -070096 if (vm.count("sign-id") != 0)
Yingdi Yub61f5402014-02-26 17:46:11 -080097 {
Yingdi Yu05842f22014-04-15 19:21:56 -070098 hasSignId = true;
Yingdi Yue6bfab22014-02-06 16:01:19 -080099 }
100
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700101 if (vm.count("subject-name") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800102 {
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700103 std::cerr << "subject_name must be specified" << std::endl;
104 return 1;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800105 }
106
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700107 std::vector<CertificateSubjectDescription> subjectDescription;
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700108 subjectDescription.push_back(CertificateSubjectDescription(oid::ATTRIBUTE_NAME, subjectName));
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700109
Yingdi Yub61f5402014-02-26 17:46:11 -0800110 tokenizer<escaped_list_separator<char> > subjectInfoItems
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700111 (subjectInfo, escaped_list_separator<char>("\\", " \t", "'\""));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800112
Yingdi Yub61f5402014-02-26 17:46:11 -0800113 tokenizer<escaped_list_separator<char> >::iterator it =
114 subjectInfoItems.begin();
115
116 while (it != subjectInfoItems.end())
Yingdi Yue6bfab22014-02-06 16:01:19 -0800117 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800118 std::string oid = *it;
119
120 it++;
121 if (it == subjectInfoItems.end())
Yingdi Yue6bfab22014-02-06 16:01:19 -0800122 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800123 std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl;
124 return 1;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800125 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800126
127 std::string value = *it;
128
Yingdi Yu9d9d5992014-06-25 12:25:16 -0700129 subjectDescription.push_back(CertificateSubjectDescription(OID(oid), value));
Yingdi Yub61f5402014-02-26 17:46:11 -0800130
131 it++;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800132 }
133
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700134 system_clock::TimePoint notBefore;
135 system_clock::TimePoint notAfter;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800136
Yingdi Yub61f5402014-02-26 17:46:11 -0800137 if (vm.count("not-before") == 0)
138 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700139 notBefore = system_clock::now();
Yingdi Yub61f5402014-02-26 17:46:11 -0800140 }
141 else
142 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700143 notBefore = fromIsoString(notBeforeStr.substr(0, 8) + "T" +
144 notBeforeStr.substr(8, 6));
Yingdi Yub61f5402014-02-26 17:46:11 -0800145 }
Yingdi Yue6bfab22014-02-06 16:01:19 -0800146
Yingdi Yub61f5402014-02-26 17:46:11 -0800147 if (vm.count("not-after") == 0)
148 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700149 notAfter = notBefore + days(365);
Yingdi Yub61f5402014-02-26 17:46:11 -0800150 }
151 else
152 {
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700153 notAfter = fromIsoString(notAfterStr.substr(0, 8) + "T" +
154 notAfterStr.substr(8, 6));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800155
Yingdi Yub61f5402014-02-26 17:46:11 -0800156 if (notAfter < notBefore)
157 {
158 std::cerr << "not-before is later than not-after" << std::endl;
159 return 1;
160 }
161 }
162
163 if (vm.count("request") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800164 {
165 std::cerr << "request file must be specified" << std::endl;
166 return 1;
167 }
168
Yingdi Yub61f5402014-02-26 17:46:11 -0800169 shared_ptr<IdentityCertificate> selfSignedCertificate
170 = getIdentityCertificate(requestFile);
171
172 if (!static_cast<bool>(selfSignedCertificate))
Yingdi Yue6bfab22014-02-06 16:01:19 -0800173 {
174 std::cerr << "ERROR: input error" << std::endl;
175 return 1;
176 }
177
Yingdi Yu05842f22014-04-15 19:21:56 -0700178 KeyChain keyChain;
179
Yingdi Yue6bfab22014-02-06 16:01:19 -0800180 Name keyName = selfSignedCertificate->getPublicKeyName();
181 Name signIdName;
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700182 Name prefix(certPrefix);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800183
Yingdi Yu05842f22014-04-15 19:21:56 -0700184 if (!hasSignId)
185 signIdName = keyChain.getDefaultIdentity();
186 else
187 signIdName = Name(signId);
188
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700189 shared_ptr<IdentityCertificate> certificate =
190 keyChain.prepareUnsignedIdentityCertificate(keyName, selfSignedCertificate->getPublicKeyInfo(),
191 signIdName, notBefore, notAfter,
192 subjectDescription, prefix);
Yingdi Yu05842f22014-04-15 19:21:56 -0700193
Yingdi Yuc4c81202014-07-08 11:07:50 -0700194 if (!static_cast<bool>(certificate))
195 {
196 std::cerr << "ERROR: key name is not formated correctly or does not match certificate name."
197 << std::endl;
198 return 1;
199 }
200
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700201 keyChain.createIdentity(signIdName);
202 Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(signIdName);
203 keyChain.sign(*certificate, signingCertificateName);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800204
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700205 Block wire = certificate->wireEncode();
Yingdi Yue6bfab22014-02-06 16:01:19 -0800206
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800207 try
208 {
209 using namespace CryptoPP;
210 StringSource ss(wire.wire(), wire.size(), true,
211 new Base64Encoder(new FileSink(std::cout), true, 64));
212 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800213 catch (const CryptoPP::Exception& e)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800214 {
215 std::cerr << "ERROR: " << e.what() << std::endl;
216 return 1;
217 }
Yingdi Yue6bfab22014-02-06 16:01:19 -0800218
219 return 0;
220}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800221
222#endif //NDNSEC_CERT_GEN_HPP