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