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