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 | /* |
| 3 | * Copyright (c) 2013-2018 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) { |
| 54 | BOOST_THROW_EXCEPTION(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) { |
| 67 | BOOST_THROW_EXCEPTION(HttpException("HTTP communication error")); |
| 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/") { |
| 79 | BOOST_THROW_EXCEPTION(HttpException("HTTP communication error")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 80 | } |
| 81 | if (statusCode != 200) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 82 | BOOST_THROW_EXCEPTION(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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 156 | try { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 157 | if (certFileName.find("http://") == 0) { |
| 158 | std::string host; |
| 159 | std::string port; |
| 160 | std::string path; |
| 161 | |
| 162 | size_t pos = 7; // offset of "http://" |
| 163 | size_t posSlash = certFileName.find("/", pos); |
| 164 | |
| 165 | if (posSlash == std::string::npos) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 166 | BOOST_THROW_EXCEPTION(HttpException("Request line is not correctly formatted")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 167 | |
| 168 | size_t posPort = certFileName.find(":", pos); |
| 169 | |
| 170 | if (posPort != std::string::npos && posPort < posSlash) { |
| 171 | // port is specified |
| 172 | port = certFileName.substr(posPort + 1, posSlash - posPort - 1); |
| 173 | host = certFileName.substr(pos, posPort - pos); |
| 174 | } |
| 175 | else { |
| 176 | port = "80"; |
| 177 | host = certFileName.substr(pos, posSlash - pos); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame] | 180 | path = certFileName.substr(posSlash, certFileName.size() - posSlash); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 181 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 182 | cert = getCertificateHttp(host, port, path); |
| 183 | } |
| 184 | else { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 185 | cert = loadCertificate(certFileName); |
| 186 | } |
| 187 | } |
| 188 | catch (const CannotLoadCertificate&) { |
| 189 | std::cerr << "ERROR: Cannot load the certificate " << certFileName << std::endl; |
| 190 | return 1; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 191 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 192 | |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 193 | security::v2::KeyChain keyChain; |
| 194 | security::Identity id; |
| 195 | security::Key key; |
| 196 | try { |
| 197 | id = keyChain.getPib().getIdentity(cert.getIdentity()); |
| 198 | key = id.getKey(cert.getKeyName()); |
| 199 | } |
| 200 | catch (const security::Pib::Error& e) { |
| 201 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 202 | } |
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 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 206 | if (isSystemDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 207 | keyChain.setDefaultIdentity(id); |
| 208 | keyChain.setDefaultKey(id, key); |
| 209 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 210 | } |
| 211 | else if (isIdentityDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 212 | keyChain.setDefaultKey(id, key); |
| 213 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 214 | } |
| 215 | else if (isKeyDefault) { |
Alexander Afanasyev | 35109a1 | 2017-01-04 15:39:06 -0800 | [diff] [blame] | 216 | keyChain.setDefaultCertificate(key, cert); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 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 |