blob: f7ce2f09d44cc43450f85836118312202d5b5b47 [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 Pesavento25d9c9f2024-06-23 15:10:05 -04003 * Copyright (c) 2013-2024 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"
Davide Pesavento25d9c9f2024-06-23 15:10:05 -040045 "Import a certificate from a file.\n"
46 "\n"
Davide Pesaventob310efb2019-04-11 22:10:24 -040047 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080048 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080049 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040050 ("cert-file,f", po::value<std::string>(&certFile),
Junxiao Shi5bd6c642022-01-10 03:49:53 +000051 "file name of the certificate to be imported, '-' for stdin")
Davide Pesaventob310efb2019-04-11 22:10:24 -040052 ("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 Yue6bfab22014-02-06 16:01:19 -080058 ;
Davide Pesaventob310efb2019-04-11 22:10:24 -040059
Yingdi Yue6bfab22014-02-06 16:01:19 -080060 po::positional_options_description p;
61 p.add("cert-file", 1);
62
63 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070064 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080065 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
66 po::notify(vm);
67 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070068 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040069 std::cerr << "ERROR: " << e.what() << "\n\n"
70 << description << std::endl;
71 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080072 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080073
Davide Pesaventob310efb2019-04-11 22:10:24 -040074 if (vm.count("help") > 0) {
75 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080076 return 0;
77 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080078
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070079 if (vm.count("cert-file") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040080 std::cerr << "ERROR: you must specify a file name" << std::endl;
81 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070082 }
83
Davide Pesaventob310efb2019-04-11 22:10:24 -040084 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 Afanasyev2fa59392016-07-29 17:24:23 -070088 }
89
Davide Pesavento949075a2021-10-17 22:07:07 -040090 if (certFile.find("http://") == 0) {
Junxiao Shi5bd6c642022-01-10 03:49:53 +000091 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 Afanasyev2fa59392016-07-29 17:24:23 -070096 }
Yingdi Yub61f5402014-02-26 17:46:11 -080097
Junxiao Shi5bd6c642022-01-10 03:49:53 +000098 auto cert = loadFromFile<security::Certificate>(certFile);
Davide Pesaventob310efb2019-04-11 22:10:24 -040099
Junxiao Shi5bd6c642022-01-10 03:49:53 +0000100 KeyChain keyChain; // open KeyChain after loading certificate
Davide Pesaventob310efb2019-04-11 22:10:24 -0400101 auto id = keyChain.getPib().getIdentity(cert.getIdentity());
102 auto key = id.getKey(cert.getKeyName());
Yingdi Yub61f5402014-02-26 17:46:11 -0800103
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800104 keyChain.addCertificate(key, cert);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400105 if (isIdentityDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800106 keyChain.setDefaultKey(id, key);
107 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700108 }
109 else if (isKeyDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800110 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700111 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400112 else if (!isNoDefault) {
113 keyChain.setDefaultIdentity(id);
114 keyChain.setDefaultKey(id, key);
115 keyChain.setDefaultCertificate(key, cert);
116 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800117
Junxiao Shi5bd6c642022-01-10 03:49:53 +0000118 std::cerr << "OK: certificate with name [" << cert.getName() << "] "
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800119 << "has been successfully installed" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800120
121 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800122}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800123
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400124} // namespace ndn::ndnsec