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 | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 38 | shared_ptr<security::v1::IdentityCertificate> |
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 | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 87 | auto identityCertificate = std::make_shared<security::v1::IdentityCertificate>(); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 88 | identityCertificate->wireDecode(ndn::Block(os.buf())); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 89 | |
| 90 | return identityCertificate; |
| 91 | } |
| 92 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 93 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 94 | ndnsec_cert_install(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 95 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 96 | using namespace ndn; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 97 | using namespace ndn::security; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 98 | namespace po = boost::program_options; |
| 99 | |
| 100 | std::string certFileName; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 101 | bool isSystemDefault = true; |
| 102 | bool isIdentityDefault = false; |
| 103 | bool isKeyDefault = false; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 104 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 105 | po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options"); |
| 106 | description.add_options() |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 107 | ("help,h", "produce help message") |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 108 | ("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] | 109 | "If starts with http://, will try to fetch " |
| 110 | "the certificate using HTTP GET request") |
| 111 | ("identity-default,I", "optional, if specified, the certificate will be set as the default certificate of the identity") |
| 112 | ("key-default,K", "optional, if specified, the certificate will be set as the default certificate of the key") |
| 113 | ("no-default,N", "optional, if specified, the certificate will be simply installed") |
| 114 | ; |
| 115 | po::positional_options_description p; |
| 116 | p.add("cert-file", 1); |
| 117 | |
| 118 | po::variables_map vm; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 119 | try { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 120 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm); |
| 121 | po::notify(vm); |
| 122 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 123 | catch (const std::exception& e) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 124 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 125 | return 1; |
| 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("help") != 0) { |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 129 | std::cerr << description << std::endl; |
| 130 | return 0; |
| 131 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 133 | if (vm.count("cert-file") == 0) { |
| 134 | std::cerr << "cert_file must be specified" << std::endl; |
| 135 | std::cerr << description << std::endl; |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | if (vm.count("identity-default") != 0) { |
| 140 | isIdentityDefault = true; |
| 141 | isSystemDefault = false; |
| 142 | } |
| 143 | else if (vm.count("key-default") != 0) { |
| 144 | isKeyDefault = true; |
| 145 | isSystemDefault = false; |
| 146 | } |
| 147 | else if (vm.count("no-default") != 0) { |
| 148 | // noDefault = true; |
| 149 | isSystemDefault = false; |
| 150 | } |
| 151 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 152 | shared_ptr<security::v1::IdentityCertificate> cert; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 153 | |
| 154 | if (certFileName.find("http://") == 0) { |
| 155 | std::string host; |
| 156 | std::string port; |
| 157 | std::string path; |
| 158 | |
| 159 | size_t pos = 7; // offset of "http://" |
| 160 | size_t posSlash = certFileName.find("/", pos); |
| 161 | |
| 162 | if (posSlash == std::string::npos) |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 163 | BOOST_THROW_EXCEPTION(HttpException("Request line is not correctly formatted")); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 164 | |
| 165 | size_t posPort = certFileName.find(":", pos); |
| 166 | |
| 167 | if (posPort != std::string::npos && posPort < posSlash) { |
| 168 | // port is specified |
| 169 | port = certFileName.substr(posPort + 1, posSlash - posPort - 1); |
| 170 | host = certFileName.substr(pos, posPort - pos); |
| 171 | } |
| 172 | else { |
| 173 | port = "80"; |
| 174 | host = certFileName.substr(pos, posSlash - pos); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 177 | path = certFileName.substr(posSlash, certFileName.size() - posSlash); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 179 | cert = getCertificateHttp(host, port, path); |
| 180 | } |
| 181 | else { |
| 182 | cert = getIdentityCertificate(certFileName); |
| 183 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 185 | if (cert == nullptr) |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 186 | return 1; |
| 187 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 188 | security::v1::KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 189 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 190 | if (isSystemDefault) { |
| 191 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 192 | Name keyName = cert->getPublicKeyName(); |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 193 | Name identity = keyName.getSubName(0, keyName.size() - 1); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 194 | keyChain.setDefaultIdentity(identity); |
| 195 | } |
| 196 | else if (isIdentityDefault) { |
| 197 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 198 | } |
| 199 | else if (isKeyDefault) { |
| 200 | keyChain.addCertificateAsKeyDefault(*cert); |
| 201 | } |
| 202 | else { |
| 203 | keyChain.addCertificate(*cert); |
| 204 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 206 | std::cerr << "OK: certificate with name [" << cert->getName().toUri() |
| 207 | << "] has been successfully installed" << std::endl; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 208 | |
| 209 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 210 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 211 | |
Alexander Afanasyev | 82c359c | 2017-01-04 14:48:07 -0800 | [diff] [blame^] | 212 | } // namespace ndnsec |
| 213 | } // namespace ndn |