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