blob: 9a13e330de60b078b369d6ff1a46014822653c1e [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/*
Junxiao Shi5bd6c642022-01-10 03:49:53 +00003 * Copyright (c) 2013-2022 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
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080030namespace ndn {
31namespace ndnsec {
32
Yingdi Yub61f5402014-02-26 17:46:11 -080033int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080034ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080035{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080036 namespace po = boost::program_options;
37
Davide Pesaventob310efb2019-04-11 22:10:24 -040038 std::string certFile;
Yingdi Yub61f5402014-02-26 17:46:11 -080039 bool isIdentityDefault = false;
40 bool isKeyDefault = false;
Davide Pesaventob310efb2019-04-11 22:10:24 -040041 bool isNoDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -080042
Davide Pesaventob310efb2019-04-11 22:10:24 -040043 po::options_description description(
44 "Usage: ndnsec cert-install [-h] [-I|-K|-N] [-f] FILE\n"
45 "\n"
46 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -080047 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080048 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -040049 ("cert-file,f", po::value<std::string>(&certFile),
Junxiao Shi5bd6c642022-01-10 03:49:53 +000050 "file name of the certificate to be imported, '-' for stdin")
Davide Pesaventob310efb2019-04-11 22:10:24 -040051 ("identity-default,I", po::bool_switch(&isIdentityDefault),
52 "set the imported certificate as the default certificate for the identity")
53 ("key-default,K", po::bool_switch(&isKeyDefault),
54 "set the imported certificate as the default certificate for the key")
55 ("no-default,N", po::bool_switch(&isNoDefault),
56 "do not change any default settings")
Yingdi Yue6bfab22014-02-06 16:01:19 -080057 ;
Davide Pesaventob310efb2019-04-11 22:10:24 -040058
Yingdi Yue6bfab22014-02-06 16:01:19 -080059 po::positional_options_description p;
60 p.add("cert-file", 1);
61
62 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070063 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080064 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
65 po::notify(vm);
66 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070067 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040068 std::cerr << "ERROR: " << e.what() << "\n\n"
69 << description << std::endl;
70 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080071 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080072
Davide Pesaventob310efb2019-04-11 22:10:24 -040073 if (vm.count("help") > 0) {
74 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080075 return 0;
76 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080077
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070078 if (vm.count("cert-file") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -040079 std::cerr << "ERROR: you must specify a file name" << std::endl;
80 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070081 }
82
Davide Pesaventob310efb2019-04-11 22:10:24 -040083 if (isIdentityDefault + isKeyDefault + isNoDefault > 1) {
84 std::cerr << "ERROR: at most one of '--identity-default', '--key-default', "
85 "or '--no-default' may be specified" << std::endl;
86 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070087 }
88
Davide Pesavento949075a2021-10-17 22:07:07 -040089 if (certFile.find("http://") == 0) {
Junxiao Shi5bd6c642022-01-10 03:49:53 +000090 std::cerr << "Downloading certificate over HTTP is no longer supported." << std::endl
91 << "Instead, please run:" << std::endl
92 << "curl -sfLS " << std::quoted(certFile, '\'', '\\')
93 << " | ndnsec cert-install -" << std::endl;
94 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070095 }
Yingdi Yub61f5402014-02-26 17:46:11 -080096
Junxiao Shi5bd6c642022-01-10 03:49:53 +000097 auto cert = loadFromFile<security::Certificate>(certFile);
Davide Pesaventob310efb2019-04-11 22:10:24 -040098
Junxiao Shi5bd6c642022-01-10 03:49:53 +000099 KeyChain keyChain; // open KeyChain after loading certificate
Davide Pesaventob310efb2019-04-11 22:10:24 -0400100 auto id = keyChain.getPib().getIdentity(cert.getIdentity());
101 auto key = id.getKey(cert.getKeyName());
Yingdi Yub61f5402014-02-26 17:46:11 -0800102
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800103 keyChain.addCertificate(key, cert);
Davide Pesaventob310efb2019-04-11 22:10:24 -0400104 if (isIdentityDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800105 keyChain.setDefaultKey(id, key);
106 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700107 }
108 else if (isKeyDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800109 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700110 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400111 else if (!isNoDefault) {
112 keyChain.setDefaultIdentity(id);
113 keyChain.setDefaultKey(id, key);
114 keyChain.setDefaultCertificate(key, cert);
115 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800116
Junxiao Shi5bd6c642022-01-10 03:49:53 +0000117 std::cerr << "OK: certificate with name [" << cert.getName() << "] "
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800118 << "has been successfully installed" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800119
120 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800121}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800122
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800123} // namespace ndnsec
124} // namespace ndn