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 | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2013-2023 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 | e6bfab2 | 2014-02-06 16:01:19 -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 | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 25 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
| 26 | #include "ndn-cxx/security/transform/base64-decode.hpp" |
| 27 | #include "ndn-cxx/security/transform/stream-sink.hpp" |
| 28 | #include "ndn-cxx/security/transform/stream-source.hpp" |
| 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 30 | namespace ndn::ndnsec { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 31 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 32 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 33 | ndnsec_cert_install(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 34 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 35 | namespace po = boost::program_options; |
| 36 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 37 | std::string certFile; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | bool isIdentityDefault = false; |
| 39 | bool isKeyDefault = false; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 40 | bool isNoDefault = false; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 41 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 42 | po::options_description description( |
| 43 | "Usage: ndnsec cert-install [-h] [-I|-K|-N] [-f] FILE\n" |
| 44 | "\n" |
| 45 | "Options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 46 | description.add_options() |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 47 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 48 | ("cert-file,f", po::value<std::string>(&certFile), |
Junxiao Shi | 5bd6c64 | 2022-01-10 03:49:53 +0000 | [diff] [blame] | 49 | "file name of the certificate to be imported, '-' for stdin") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 50 | ("identity-default,I", po::bool_switch(&isIdentityDefault), |
| 51 | "set the imported certificate as the default certificate for the identity") |
| 52 | ("key-default,K", po::bool_switch(&isKeyDefault), |
| 53 | "set the imported certificate as the default certificate for the key") |
| 54 | ("no-default,N", po::bool_switch(&isNoDefault), |
| 55 | "do not change any default settings") |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 56 | ; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 57 | |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 58 | po::positional_options_description p; |
| 59 | p.add("cert-file", 1); |
| 60 | |
| 61 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 62 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 63 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 64 | po::notify(vm); |
| 65 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 66 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 67 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 68 | << description << std::endl; |
| 69 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 70 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 71 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 72 | if (vm.count("help") > 0) { |
| 73 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 74 | return 0; |
| 75 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 77 | if (vm.count("cert-file") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 78 | std::cerr << "ERROR: you must specify a file name" << std::endl; |
| 79 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 82 | if (isIdentityDefault + isKeyDefault + isNoDefault > 1) { |
| 83 | std::cerr << "ERROR: at most one of '--identity-default', '--key-default', " |
| 84 | "or '--no-default' may be specified" << std::endl; |
| 85 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Davide Pesavento | 949075a | 2021-10-17 22:07:07 -0400 | [diff] [blame] | 88 | if (certFile.find("http://") == 0) { |
Junxiao Shi | 5bd6c64 | 2022-01-10 03:49:53 +0000 | [diff] [blame] | 89 | std::cerr << "Downloading certificate over HTTP is no longer supported." << std::endl |
| 90 | << "Instead, please run:" << std::endl |
| 91 | << "curl -sfLS " << std::quoted(certFile, '\'', '\\') |
| 92 | << " | ndnsec cert-install -" << std::endl; |
| 93 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 94 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 95 | |
Junxiao Shi | 5bd6c64 | 2022-01-10 03:49:53 +0000 | [diff] [blame] | 96 | auto cert = loadFromFile<security::Certificate>(certFile); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 97 | |
Junxiao Shi | 5bd6c64 | 2022-01-10 03:49:53 +0000 | [diff] [blame] | 98 | KeyChain keyChain; // open KeyChain after loading certificate |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 99 | auto id = keyChain.getPib().getIdentity(cert.getIdentity()); |
| 100 | auto key = id.getKey(cert.getKeyName()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 102 | keyChain.addCertificate(key, cert); |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 103 | if (isIdentityDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 104 | keyChain.setDefaultKey(id, key); |
| 105 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 106 | } |
| 107 | else if (isKeyDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 108 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 109 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 110 | else if (!isNoDefault) { |
| 111 | keyChain.setDefaultIdentity(id); |
| 112 | keyChain.setDefaultKey(id, key); |
| 113 | keyChain.setDefaultCertificate(key, cert); |
| 114 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 115 | |
Junxiao Shi | 5bd6c64 | 2022-01-10 03:49:53 +0000 | [diff] [blame] | 116 | std::cerr << "OK: certificate with name [" << cert.getName() << "] " |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 117 | << "has been successfully installed" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 118 | |
| 119 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 120 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 121 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 122 | } // namespace ndn::ndnsec |