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