blob: f4655afeac6b3443cc2acb2b4dc96cbb17ec4742 [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 Pesaventoa3d809e2022-02-06 11:55:02 -05003 * Copyright (c) 2013-2022 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
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080025namespace ndn {
26namespace ndnsec {
27
Yingdi Yub61f5402014-02-26 17:46:11 -080028int
29ndnsec_sign_req(int argc, char** argv)
Yingdi Yu8d7468f2014-02-21 14:49:45 -080030{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031 namespace po = boost::program_options;
32
Alexander Afanasyev35109a12017-01-04 15:39:06 -080033 Name name;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034 bool isKeyName = false;
35
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080036 po::options_description description(
Davide Pesaventob310efb2019-04-11 22:10:24 -040037 "Usage: ndnsec sign-req [-h] [-k] [-n] NAME\n"
38 "\n"
39 "Options");
40 description.add_options()
Yingdi Yu8d7468f2014-02-21 14:49:45 -080041 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 ("name,n", po::value<Name>(&name), "identity or key name, e.g., /ndn/edu/ucla/alice")
43 ("key,k", po::bool_switch(&isKeyName), "the specified name is a key name rather than an identity")
44 ;
Yingdi Yu8d7468f2014-02-21 14:49:45 -080045
46 po::positional_options_description p;
47 p.add("name", 1);
48
49 po::variables_map vm;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080050 try {
51 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
52 po::notify(vm);
53 }
54 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040055 std::cerr << "ERROR: " << e.what() << "\n\n"
56 << description << std::endl;
57 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080058 }
Yingdi Yub61f5402014-02-26 17:46:11 -080059
Davide Pesaventob310efb2019-04-11 22:10:24 -040060 if (vm.count("help") > 0) {
61 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080062 return 0;
63 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080064
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080065 if (vm.count("name") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040066 std::cerr << "ERROR: you must specify a name" << std::endl;
67 return 2;
Yingdi Yu37339fd2014-11-26 22:26:43 -080068 }
Yingdi Yu8d7468f2014-02-21 14:49:45 -080069
Davide Pesaventof2cae612021-03-24 18:47:05 -040070 KeyChain keyChain;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080071
72 security::Identity identity;
73 security::Key key;
74 if (!isKeyName) {
75 identity = keyChain.getPib().getIdentity(name);
76 key = identity.getDefaultKey();
Yingdi Yu37339fd2014-11-26 22:26:43 -080077 }
78 else {
Davide Pesaventof2cae612021-03-24 18:47:05 -040079 identity = keyChain.getPib().getIdentity(security::extractIdentityFromKeyName(name));
Alexander Afanasyev35109a12017-01-04 15:39:06 -080080 key = identity.getKey(name);
Yingdi Yu37339fd2014-11-26 22:26:43 -080081 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -080082
83 // Create signing request (similar to self-signed certificate)
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 security::Certificate certificate;
Alexander Afanasyev35109a12017-01-04 15:39:06 -080085
86 // set name
87 Name certificateName = key.getName();
88 certificateName
89 .append("cert-request")
90 .appendVersion();
91 certificate.setName(certificateName);
92
93 // set metainfo
94 certificate.setContentType(tlv::ContentType_Key);
Davide Pesavento0f830802018-01-16 23:58:58 -050095 certificate.setFreshnessPeriod(1_h);
Alexander Afanasyev35109a12017-01-04 15:39:06 -080096
97 // set content
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050098 certificate.setContent(key.getPublicKey());
Alexander Afanasyev35109a12017-01-04 15:39:06 -080099
100 // set signature-info
101 SignatureInfo signatureInfo;
Davide Pesavento0f830802018-01-16 23:58:58 -0500102 auto now = time::system_clock::now();
103 signatureInfo.setValidityPeriod(security::ValidityPeriod(now, now + 10_days));
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104
105 keyChain.sign(certificate, security::SigningInfo(key).setSignatureInfo(signatureInfo));
106
107 io::save(certificate, std::cout);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400108
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800109 return 0;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800110}
111
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800112} // namespace ndnsec
113} // namespace ndn