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