blob: ca538199dd4ff86ea00947b2d5a6546f1b165ff3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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.
Yingdi Yu8d7468f2014-02-21 14:49:45 -080020 */
21
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080022#include "ndnsec.hpp"
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080023#include "util.hpp"
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024
Junxiao Shi9ee770b2022-04-25 23:33:33 +000025#include "ndn-cxx/security/signing-helpers.hpp"
26
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029int
30ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080032 namespace po = boost::program_options;
33
Alexander Afanasyev35109a12017-01-04 15:39:06 -080034 Name name;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 bool isKeyName = false;
36
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080037 po::options_description description(
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 "Usage: ndnsec sign-req [-h] [-k] [-n] NAME\n"
39 "\n"
40 "Options");
41 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080042 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 ("name,n", po::value<Name>(&name), "identity or key name, e.g., /ndn/edu/ucla/alice")
44 ("key,k", po::bool_switch(&isKeyName), "the specified name is a key name rather than an identity")
45 ;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080046
47 po::positional_options_description p;
48 p.add("name", 1);
49
50 po::variables_map vm;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080051 try {
52 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
53 po::notify(vm);
54 }
55 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040056 std::cerr << "ERROR: " << e.what() << "\n\n"
57 << description << std::endl;
58 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080059 }
Yingdi Yub61f5402014-02-26 17:46:11 -080060
Davide Pesaventob310efb2019-04-11 22:10:24 -040061 if (vm.count("help") > 0) {
62 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 return 0;
64 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080065
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080066 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 std::cerr << "ERROR: you must specify a name" << std::endl;
68 return 2;
Yingdi Yu37339fd2014-11-26 22:26:43 -080069 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080070
Davide Pesaventof2cae612021-03-24 18:47:05 -040071 KeyChain keyChain;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080072
73 security::Identity identity;
74 security::Key key;
75 if (!isKeyName) {
76 identity = keyChain.getPib().getIdentity(name);
77 key = identity.getDefaultKey();
Yingdi Yu37339fd2014-11-26 22:26:43 -080078 }
79 else {
Davide Pesaventof2cae612021-03-24 18:47:05 -040080 identity = keyChain.getPib().getIdentity(security::extractIdentityFromKeyName(name));
Alexander Afanasyev35109a12017-01-04 15:39:06 -080081 key = identity.getKey(name);
Yingdi Yu37339fd2014-11-26 22:26:43 -080082 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080083
84 // Create signing request (similar to self-signed certificate)
Junxiao Shi9ee770b2022-04-25 23:33:33 +000085 security::MakeCertificateOptions opts;
86 opts.issuerId = name::Component::fromEscapedString("cert-request");
87 opts.validity = security::ValidityPeriod::makeRelative(-1_s, 10_days);
88 auto certificate = keyChain.makeCertificate(key, security::signingByKey(key), opts);
Alexander Afanasyev35109a12017-01-04 15:39:06 -080089
90 io::save(certificate, std::cout);
Davide Pesaventob310efb2019-04-11 22:10:24 -040091
Alexander Afanasyev35109a12017-01-04 15:39:06 -080092 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080093}
94
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040095} // namespace ndn::ndnsec