blob: 7a6afa2e65fbbb5d38c7f100768c320098859d30 [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 Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 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
30#include <boost/asio/ip/tcp.hpp>
Davide Pesavento6d433932018-04-17 18:35:00 -040031#if BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050032#include <boost/date_time/posix_time/posix_time_duration.hpp>
Davide Pesavento6d433932018-04-17 18:35:00 -040033#endif // BOOST_VERSION < 106700
Davide Pesavento5afbb0b2018-01-01 17:24:18 -050034
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080035namespace ndn {
36namespace ndnsec {
37
Yingdi Yub61f5402014-02-26 17:46:11 -080038class HttpException : public std::runtime_error
Yingdi Yue6bfab22014-02-06 16:01:19 -080039{
Yingdi Yub61f5402014-02-26 17:46:11 -080040public:
41 explicit
42 HttpException(const std::string& what)
43 : std::runtime_error(what)
Yingdi Yue6bfab22014-02-06 16:01:19 -080044 {
45 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080046};
47
Davide Pesaventof2cae612021-03-24 18:47:05 -040048static security::Certificate
Yingdi Yub61f5402014-02-26 17:46:11 -080049getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
Yingdi Yue6bfab22014-02-06 16:01:19 -080050{
Davide Pesavento6d433932018-04-17 18:35:00 -040051 boost::asio::ip::tcp::iostream requestStream;
52#if BOOST_VERSION >= 106700
Davide Pesaventob310efb2019-04-11 22:10:24 -040053 requestStream.expires_after(std::chrono::seconds(10));
Davide Pesavento6d433932018-04-17 18:35:00 -040054#else
Davide Pesaventob310efb2019-04-11 22:10:24 -040055 requestStream.expires_from_now(boost::posix_time::seconds(10));
Davide Pesavento6d433932018-04-17 18:35:00 -040056#endif // BOOST_VERSION >= 106700
57
Yingdi Yub61f5402014-02-26 17:46:11 -080058 requestStream.connect(host, port);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080059 if (!requestStream) {
Davide Pesavento923ba442019-02-12 22:00:38 -050060 NDN_THROW(HttpException("HTTP connection error"));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070061 }
Davide Pesavento6d433932018-04-17 18:35:00 -040062
Yingdi Yub61f5402014-02-26 17:46:11 -080063 requestStream << "GET " << path << " HTTP/1.0\r\n";
64 requestStream << "Host: " << host << "\r\n";
65 requestStream << "Accept: */*\r\n";
66 requestStream << "Cache-Control: no-cache\r\n";
67 requestStream << "Connection: close\r\n\r\n";
68 requestStream.flush();
Yingdi Yue6bfab22014-02-06 16:01:19 -080069
Yingdi Yub61f5402014-02-26 17:46:11 -080070 std::string statusLine;
71 std::getline(requestStream, statusLine);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080072 if (!requestStream) {
Davide Pesavento923ba442019-02-12 22:00:38 -050073 NDN_THROW(HttpException("HTTP communication error"));
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080074 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080075
Yingdi Yub61f5402014-02-26 17:46:11 -080076 std::stringstream responseStream(statusLine);
77 std::string httpVersion;
78 responseStream >> httpVersion;
79 unsigned int statusCode;
80 responseStream >> statusCode;
81 std::string statusMessage;
Yingdi Yue6bfab22014-02-06 16:01:19 -080082
Yingdi Yub61f5402014-02-26 17:46:11 -080083 std::getline(responseStream, statusMessage);
Alexander Afanasyev82c359c2017-01-04 14:48:07 -080084 if (!requestStream || httpVersion.substr(0, 5) != "HTTP/") {
Davide Pesavento923ba442019-02-12 22:00:38 -050085 NDN_THROW(HttpException("HTTP communication error"));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070086 }
87 if (statusCode != 200) {
Davide Pesavento923ba442019-02-12 22:00:38 -050088 NDN_THROW(HttpException("HTTP server error"));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070089 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080090 std::string header;
Yingdi Yub61f5402014-02-26 17:46:11 -080091 while (std::getline(requestStream, header) && header != "\r")
92 ;
Yingdi Yue6bfab22014-02-06 16:01:19 -080093
Davide Pesaventob310efb2019-04-11 22:10:24 -040094 OBufferStream os;
Yingdi Yub61f5402014-02-26 17:46:11 -080095 {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070096 using namespace ndn::security::transform;
97 streamSource(requestStream) >> base64Decode(true) >> streamSink(os);
Yingdi Yub61f5402014-02-26 17:46:11 -080098 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080099
Davide Pesaventof2cae612021-03-24 18:47:05 -0400100 return security::Certificate(Block(os.buf()));
Yingdi Yue6bfab22014-02-06 16:01:19 -0800101}
102
Yingdi Yub61f5402014-02-26 17:46:11 -0800103int
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800104ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800105{
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800106 namespace po = boost::program_options;
107
Davide Pesaventob310efb2019-04-11 22:10:24 -0400108 std::string certFile;
Yingdi Yub61f5402014-02-26 17:46:11 -0800109 bool isIdentityDefault = false;
110 bool isKeyDefault = false;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400111 bool isNoDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800112
Davide Pesaventob310efb2019-04-11 22:10:24 -0400113 po::options_description description(
114 "Usage: ndnsec cert-install [-h] [-I|-K|-N] [-f] FILE\n"
115 "\n"
116 "Options");
Yingdi Yub61f5402014-02-26 17:46:11 -0800117 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -0800118 ("help,h", "produce help message")
Davide Pesaventob310efb2019-04-11 22:10:24 -0400119 ("cert-file,f", po::value<std::string>(&certFile),
120 "file name of the certificate to be imported, '-' for stdin; "
121 "if it starts with 'http://', the certificate will be fetched "
122 "using a plain HTTP/1.0 GET request")
123 ("identity-default,I", po::bool_switch(&isIdentityDefault),
124 "set the imported certificate as the default certificate for the identity")
125 ("key-default,K", po::bool_switch(&isKeyDefault),
126 "set the imported certificate as the default certificate for the key")
127 ("no-default,N", po::bool_switch(&isNoDefault),
128 "do not change any default settings")
Yingdi Yue6bfab22014-02-06 16:01:19 -0800129 ;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400130
Yingdi Yue6bfab22014-02-06 16:01:19 -0800131 po::positional_options_description p;
132 p.add("cert-file", 1);
133
134 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700135 try {
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800136 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
137 po::notify(vm);
138 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700139 catch (const std::exception& e) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400140 std::cerr << "ERROR: " << e.what() << "\n\n"
141 << description << std::endl;
142 return 2;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800143 }
Yingdi Yue6bfab22014-02-06 16:01:19 -0800144
Davide Pesaventob310efb2019-04-11 22:10:24 -0400145 if (vm.count("help") > 0) {
146 std::cout << description << std::endl;
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800147 return 0;
148 }
Yingdi Yue6bfab22014-02-06 16:01:19 -0800149
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700150 if (vm.count("cert-file") == 0) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400151 std::cerr << "ERROR: you must specify a file name" << std::endl;
152 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700153 }
154
Davide Pesaventob310efb2019-04-11 22:10:24 -0400155 if (isIdentityDefault + isKeyDefault + isNoDefault > 1) {
156 std::cerr << "ERROR: at most one of '--identity-default', '--key-default', "
157 "or '--no-default' may be specified" << std::endl;
158 return 2;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700159 }
160
Davide Pesaventof2cae612021-03-24 18:47:05 -0400161 security::Certificate cert;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800162 try {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400163 if (certFile.find("http://") == 0) {
Davide Pesavento923ba442019-02-12 22:00:38 -0500164 std::string host;
165 std::string port;
166 std::string path;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700167
Davide Pesavento923ba442019-02-12 22:00:38 -0500168 size_t pos = 7; // offset of "http://"
Davide Pesaventof2cae612021-03-24 18:47:05 -0400169 size_t posSlash = certFile.find('/', pos);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700170
Davide Pesavento923ba442019-02-12 22:00:38 -0500171 if (posSlash == std::string::npos)
172 NDN_THROW(HttpException("Request line is not correctly formatted"));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700173
Davide Pesaventof2cae612021-03-24 18:47:05 -0400174 size_t posPort = certFile.find(':', pos);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700175
Davide Pesavento923ba442019-02-12 22:00:38 -0500176 if (posPort != std::string::npos && posPort < posSlash) {
177 // port is specified
Davide Pesaventob310efb2019-04-11 22:10:24 -0400178 port = certFile.substr(posPort + 1, posSlash - posPort - 1);
179 host = certFile.substr(pos, posPort - pos);
Davide Pesavento923ba442019-02-12 22:00:38 -0500180 }
181 else {
182 port = "80";
Davide Pesaventob310efb2019-04-11 22:10:24 -0400183 host = certFile.substr(pos, posSlash - pos);
Davide Pesavento923ba442019-02-12 22:00:38 -0500184 }
185
Davide Pesaventob310efb2019-04-11 22:10:24 -0400186 path = certFile.substr(posSlash, certFile.size() - posSlash);
Davide Pesavento923ba442019-02-12 22:00:38 -0500187
188 cert = getCertificateHttp(host, port, path);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700189 }
190 else {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400191 cert = loadCertificate(certFile);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800192 }
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800193 }
194 catch (const CannotLoadCertificate&) {
Davide Pesaventob310efb2019-04-11 22:10:24 -0400195 std::cerr << "ERROR: Cannot load the certificate from `" << certFile << "`" << std::endl;
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800196 return 1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700197 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800198
Davide Pesaventof2cae612021-03-24 18:47:05 -0400199 KeyChain keyChain;
Davide Pesaventob310efb2019-04-11 22:10:24 -0400200
201 auto id = keyChain.getPib().getIdentity(cert.getIdentity());
202 auto key = id.getKey(cert.getKeyName());
Yingdi Yub61f5402014-02-26 17:46:11 -0800203
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800204 keyChain.addCertificate(key, cert);
Yingdi Yub61f5402014-02-26 17:46:11 -0800205
Davide Pesaventob310efb2019-04-11 22:10:24 -0400206 if (isIdentityDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800207 keyChain.setDefaultKey(id, key);
208 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700209 }
210 else if (isKeyDefault) {
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800211 keyChain.setDefaultCertificate(key, cert);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700212 }
Davide Pesaventob310efb2019-04-11 22:10:24 -0400213 else if (!isNoDefault) {
214 keyChain.setDefaultIdentity(id);
215 keyChain.setDefaultKey(id, key);
216 keyChain.setDefaultCertificate(key, cert);
217 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800218
Alexander Afanasyev35109a12017-01-04 15:39:06 -0800219 std::cerr << "OK: certificate with name [" << cert.getName().toUri() << "] "
220 << "has been successfully installed" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800221
222 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800223}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800224
Alexander Afanasyev82c359c2017-01-04 14:48:07 -0800225} // namespace ndnsec
226} // namespace ndn