blob: 345ee24634e58a55a4630b2c76d08fb2b7c4f4c9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5afbb0b2018-01-01 17:24:18 -05002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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 Yue6bfab22014-02-06 16:01:19 -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 Yue6bfab22014-02-06 16:01:19 -080024
Davide Pesaventob310efb2019-04-11 22:10:24 -040025#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 Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::ndnsec {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080031
Yingdi Yub61f5402014-02-26 17:46:11 -080032int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080033ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080034{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080035 namespace po = boost::program_options;
36
Davide Pesaventob310efb2019-04-11 22:10:24 -040037 std::string certFile;
Yingdi Yub61f5402014-02-26 17:46:11 -080038 bool isIdentityDefault = false;
39 bool isKeyDefault = false;
Davide Pesaventob310efb2019-04-11 22:10:24 -040040 bool isNoDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -080041
Davide Pesaventob310efb2019-04-11 22:10:24 -040042 po::options_description description(
43 "Usage: ndnsec cert-install [-h] [-I|-K|-N] [-f] FILE\n"
44 "\n"
45 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080046 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080047 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040048 ("cert-file,f", po::value<std::string>(&certFile),
Junxiao Shi5bd6c642022-01-10 03:49:53 +000049 "file name of the certificate to be imported, '-' for stdin")
Davide Pesaventob310efb2019-04-11 22:10:24 -040050 ("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 Yue6bfab22014-02-06 16:01:19 -080056 ;
Davide Pesaventob310efb2019-04-11 22:10:24 -040057
Yingdi Yue6bfab22014-02-06 16:01:19 -080058 po::positional_options_description p;
59 p.add("cert-file", 1);
60
61 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070062 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080063 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
64 po::notify(vm);
65 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070066 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040067 std::cerr << "ERROR: " << e.what() << "\n\n"
68 << description << std::endl;
69 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080070 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080071
Davide Pesaventob310efb2019-04-11 22:10:24 -040072 if (vm.count("help") > 0) {
73 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 return 0;
75 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080076
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070077 if (vm.count("cert-file") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040078 std::cerr << "ERROR: you must specify a file name" << std::endl;
79 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070080 }
81
Davide Pesaventob310efb2019-04-11 22:10:24 -040082 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 Afanasyev2fa59392016-07-29 17:24:23 -070086 }
87
Davide Pesavento949075a2021-10-17 22:07:07 -040088 if (certFile.find("http://") == 0) {
Junxiao Shi5bd6c642022-01-10 03:49:53 +000089 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 Afanasyev2fa59392016-07-29 17:24:23 -070094 }
Yingdi Yub61f5402014-02-26 17:46:11 -080095
Junxiao Shi5bd6c642022-01-10 03:49:53 +000096 auto cert = loadFromFile<security::Certificate>(certFile);
Davide Pesaventob310efb2019-04-11 22:10:24 -040097
Junxiao Shi5bd6c642022-01-10 03:49:53 +000098 KeyChain keyChain; // open KeyChain after loading certificate
Davide Pesaventob310efb2019-04-11 22:10:24 -040099 auto id = keyChain.getPib().getIdentity(cert.getIdentity());
100 auto key = id.getKey(cert.getKeyName());
Yingdi Yub61f5402014-02-26 17:46:11 -0800101
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800102 keyChain.addCertificate(key, cert);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400103 if (isIdentityDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104 keyChain.setDefaultKey(id, key);
105 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700106 }
107 else if (isKeyDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800108 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700109 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400110 else if (!isNoDefault) {
111 keyChain.setDefaultIdentity(id);
112 keyChain.setDefaultKey(id, key);
113 keyChain.setDefaultCertificate(key, cert);
114 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800115
Junxiao Shi5bd6c642022-01-10 03:49:53 +0000116 std::cerr << "OK: certificate with name [" << cert.getName() << "] "
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800117 << "has been successfully installed" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800118
119 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800120}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800121
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400122} // namespace ndn::ndnsec