Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2021 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. |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 22 | #include "ndnsec.hpp" |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 23 | #include "util.hpp" |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | 25d4f1c | 2020-04-29 23:31:04 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/util/io.hpp" |
| 26 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 27 | namespace ndn { |
| 28 | namespace ndnsec { |
| 29 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 30 | int |
| 31 | ndnsec_sign_req(int argc, char** argv) |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 32 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | namespace po = boost::program_options; |
| 34 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 35 | Name name; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 36 | bool isKeyName = false; |
| 37 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 38 | po::options_description description( |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 39 | "Usage: ndnsec sign-req [-h] [-k] [-n] NAME\n" |
| 40 | "\n" |
| 41 | "Options"); |
| 42 | description.add_options() |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 43 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 44 | ("name,n", po::value<Name>(&name), "identity or key name, e.g., /ndn/edu/ucla/alice") |
| 45 | ("key,k", po::bool_switch(&isKeyName), "the specified name is a key name rather than an identity") |
| 46 | ; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 47 | |
| 48 | po::positional_options_description p; |
| 49 | p.add("name", 1); |
| 50 | |
| 51 | po::variables_map vm; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 52 | try { |
| 53 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 54 | po::notify(vm); |
| 55 | } |
| 56 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 57 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 58 | << description << std::endl; |
| 59 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 60 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 61 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 62 | if (vm.count("help") > 0) { |
| 63 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 64 | return 0; |
| 65 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 67 | if (vm.count("name") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 68 | std::cerr << "ERROR: you must specify a name" << std::endl; |
| 69 | return 2; |
Yingdi Yu | 37339fd | 2014-11-26 22:26:43 -0800 | [diff] [blame] | 70 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 71 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 72 | KeyChain keyChain; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 73 | |
| 74 | security::Identity identity; |
| 75 | security::Key key; |
| 76 | if (!isKeyName) { |
| 77 | identity = keyChain.getPib().getIdentity(name); |
| 78 | key = identity.getDefaultKey(); |
Yingdi Yu | 37339fd | 2014-11-26 22:26:43 -0800 | [diff] [blame] | 79 | } |
| 80 | else { |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 81 | identity = keyChain.getPib().getIdentity(security::extractIdentityFromKeyName(name)); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 82 | key = identity.getKey(name); |
Yingdi Yu | 37339fd | 2014-11-26 22:26:43 -0800 | [diff] [blame] | 83 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 84 | |
| 85 | // Create signing request (similar to self-signed certificate) |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 86 | security::Certificate certificate; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 87 | |
| 88 | // set name |
| 89 | Name certificateName = key.getName(); |
| 90 | certificateName |
| 91 | .append("cert-request") |
| 92 | .appendVersion(); |
| 93 | certificate.setName(certificateName); |
| 94 | |
| 95 | // set metainfo |
| 96 | certificate.setContentType(tlv::ContentType_Key); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 97 | certificate.setFreshnessPeriod(1_h); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 98 | |
| 99 | // set content |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 100 | certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size()); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 101 | |
| 102 | // set signature-info |
| 103 | SignatureInfo signatureInfo; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 104 | auto now = time::system_clock::now(); |
| 105 | signatureInfo.setValidityPeriod(security::ValidityPeriod(now, now + 10_days)); |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 106 | |
| 107 | keyChain.sign(certificate, security::SigningInfo(key).setSignatureInfo(signatureInfo)); |
| 108 | |
| 109 | io::save(certificate, std::cout); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 110 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 111 | return 0; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 114 | } // namespace ndnsec |
| 115 | } // namespace ndn |