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 | /* |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 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 | |
| 30 | #include <boost/asio/ip/tcp.hpp> |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 31 | #if BOOST_VERSION < 106700 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 32 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 33 | #endif // BOOST_VERSION < 106700 |
Davide Pesavento | 5afbb0b | 2018-01-01 17:24:18 -0500 | [diff] [blame] | 34 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 35 | namespace ndn { |
| 36 | namespace ndnsec { |
| 37 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 38 | class HttpException : public std::runtime_error |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 39 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | public: |
| 41 | explicit |
| 42 | HttpException(const std::string& what) |
| 43 | : std::runtime_error(what) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 48 | security::v2::Certificate |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 49 | getCertificateHttp(const std::string& host, const std::string& port, const std::string& path) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 50 | { |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 51 | boost::asio::ip::tcp::iostream requestStream; |
| 52 | #if BOOST_VERSION >= 106700 |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 53 | requestStream.expires_after(std::chrono::seconds(10)); |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 54 | #else |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 55 | requestStream.expires_from_now(boost::posix_time::seconds(10)); |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 56 | #endif // BOOST_VERSION >= 106700 |
| 57 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | requestStream.connect(host, port); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 59 | if (!requestStream) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 60 | NDN_THROW(HttpException("HTTP connection error")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 61 | } |
Davide Pesavento | 6d43393 | 2018-04-17 18:35:00 -0400 | [diff] [blame] | 62 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 63 | 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 Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 69 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 70 | std::string statusLine; |
| 71 | std::getline(requestStream, statusLine); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 72 | if (!requestStream) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 73 | NDN_THROW(HttpException("HTTP communication error")); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 74 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 75 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 76 | std::stringstream responseStream(statusLine); |
| 77 | std::string httpVersion; |
| 78 | responseStream >> httpVersion; |
| 79 | unsigned int statusCode; |
| 80 | responseStream >> statusCode; |
| 81 | std::string statusMessage; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 82 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 83 | std::getline(responseStream, statusMessage); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 84 | if (!requestStream || httpVersion.substr(0, 5) != "HTTP/") { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 85 | NDN_THROW(HttpException("HTTP communication error")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 86 | } |
| 87 | if (statusCode != 200) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 88 | NDN_THROW(HttpException("HTTP server error")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 89 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 90 | std::string header; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 91 | while (std::getline(requestStream, header) && header != "\r") |
| 92 | ; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 93 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 94 | OBufferStream os; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 95 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 96 | using namespace ndn::security::transform; |
| 97 | streamSource(requestStream) >> base64Decode(true) >> streamSink(os); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 98 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 100 | return security::v2::Certificate(Block(os.buf())); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 103 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 104 | ndnsec_cert_install(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 105 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 106 | namespace po = boost::program_options; |
| 107 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 108 | std::string certFile; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 109 | bool isIdentityDefault = false; |
| 110 | bool isKeyDefault = false; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 111 | bool isNoDefault = false; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 112 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 113 | po::options_description description( |
| 114 | "Usage: ndnsec cert-install [-h] [-I|-K|-N] [-f] FILE\n" |
| 115 | "\n" |
| 116 | "Options"); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 117 | description.add_options() |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 118 | ("help,h", "produce help message") |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 119 | ("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 Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 129 | ; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 130 | |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 131 | po::positional_options_description p; |
| 132 | p.add("cert-file", 1); |
| 133 | |
| 134 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 135 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 136 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 137 | po::notify(vm); |
| 138 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 139 | catch (const std::exception& e) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 140 | std::cerr << "ERROR: " << e.what() << "\n\n" |
| 141 | << description << std::endl; |
| 142 | return 2; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 143 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 144 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 145 | if (vm.count("help") > 0) { |
| 146 | std::cout << description << std::endl; |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 147 | return 0; |
| 148 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 150 | if (vm.count("cert-file") == 0) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 151 | std::cerr << "ERROR: you must specify a file name" << std::endl; |
| 152 | return 2; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 155 | 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 Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 161 | security::v2::Certificate cert; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 162 | try { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 163 | if (certFile.find("http://") == 0) { |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 164 | std::string host; |
| 165 | std::string port; |
| 166 | std::string path; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 167 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 168 | size_t pos = 7; // offset of "http://" |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 169 | size_t posSlash = certFile.find("/", pos); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 170 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 171 | if (posSlash == std::string::npos) |
| 172 | NDN_THROW(HttpException("Request line is not correctly formatted")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 173 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 174 | size_t posPort = certFile.find(":", pos); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 175 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 176 | if (posPort != std::string::npos && posPort < posSlash) { |
| 177 | // port is specified |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 178 | port = certFile.substr(posPort + 1, posSlash - posPort - 1); |
| 179 | host = certFile.substr(pos, posPort - pos); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 180 | } |
| 181 | else { |
| 182 | port = "80"; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 183 | host = certFile.substr(pos, posSlash - pos); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 186 | path = certFile.substr(posSlash, certFile.size() - posSlash); |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 187 | |
| 188 | cert = getCertificateHttp(host, port, path); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 189 | } |
| 190 | else { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 191 | cert = loadCertificate(certFile); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 192 | } |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 193 | } |
| 194 | catch (const CannotLoadCertificate&) { |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 195 | std::cerr << "ERROR: Cannot load the certificate from `" << certFile << "`" << std::endl; |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 196 | return 1; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 197 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 198 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 199 | security::v2::KeyChain keyChain; |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 200 | |
| 201 | auto id = keyChain.getPib().getIdentity(cert.getIdentity()); |
| 202 | auto key = id.getKey(cert.getKeyName()); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 203 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 204 | keyChain.addCertificate(key, cert); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 205 | |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 206 | if (isIdentityDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 207 | keyChain.setDefaultKey(id, key); |
| 208 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 209 | } |
| 210 | else if (isKeyDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 211 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 212 | } |
Davide Pesavento | b310efb | 2019-04-11 22:10:24 -0400 | [diff] [blame] | 213 | else if (!isNoDefault) { |
| 214 | keyChain.setDefaultIdentity(id); |
| 215 | keyChain.setDefaultKey(id, key); |
| 216 | keyChain.setDefaultCertificate(key, cert); |
| 217 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 219 | std::cerr << "OK: certificate with name [" << cert.getName().toUri() << "] " |
| 220 | << "has been successfully installed" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 221 | |
| 222 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 223 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 224 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 225 | } // namespace ndnsec |
| 226 | } // namespace ndn |