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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 24 | #ifndef NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP |
| 25 | #define NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 27 | #include "util.hpp" |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 28 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 29 | class HttpException : public std::runtime_error |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 30 | { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 31 | public: |
| 32 | explicit |
| 33 | HttpException(const std::string& what) |
| 34 | : std::runtime_error(what) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 35 | { |
| 36 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 39 | ndn::shared_ptr<ndn::security::v1::IdentityCertificate> |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 40 | 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] | 41 | { |
| 42 | using namespace boost::asio::ip; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 43 | tcp::iostream requestStream; |
| 44 | |
| 45 | requestStream.expires_from_now(boost::posix_time::milliseconds(3000)); |
| 46 | |
| 47 | requestStream.connect(host, port); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 48 | if (!static_cast<bool>(requestStream)) { |
| 49 | throw HttpException("HTTP connection error"); |
| 50 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 51 | requestStream << "GET " << path << " HTTP/1.0\r\n"; |
| 52 | requestStream << "Host: " << host << "\r\n"; |
| 53 | requestStream << "Accept: */*\r\n"; |
| 54 | requestStream << "Cache-Control: no-cache\r\n"; |
| 55 | requestStream << "Connection: close\r\n\r\n"; |
| 56 | requestStream.flush(); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 57 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 58 | std::string statusLine; |
| 59 | std::getline(requestStream, statusLine); |
| 60 | if (!static_cast<bool>(requestStream)) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 61 | { |
| 62 | throw HttpException("HTTP communication error"); |
| 63 | } |
| 64 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 65 | std::stringstream responseStream(statusLine); |
| 66 | std::string httpVersion; |
| 67 | responseStream >> httpVersion; |
| 68 | unsigned int statusCode; |
| 69 | responseStream >> statusCode; |
| 70 | std::string statusMessage; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 71 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 72 | std::getline(responseStream, statusMessage); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 73 | if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/") { |
| 74 | throw HttpException("HTTP communication error"); |
| 75 | } |
| 76 | if (statusCode != 200) { |
| 77 | throw HttpException("HTTP server error"); |
| 78 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 79 | std::string header; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 80 | while (std::getline(requestStream, header) && header != "\r") |
| 81 | ; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 82 | |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 83 | ndn::OBufferStream os; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 84 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 85 | using namespace ndn::security::transform; |
| 86 | streamSource(requestStream) >> base64Decode(true) >> streamSink(os); |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 87 | } |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 89 | auto identityCertificate = std::make_shared<ndn::security::v1::IdentityCertificate>(); |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 90 | identityCertificate->wireDecode(ndn::Block(os.buf())); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 91 | |
| 92 | return identityCertificate; |
| 93 | } |
| 94 | |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 95 | int |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 96 | ndnsec_cert_install(int argc, char** argv) |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 97 | { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 98 | using namespace ndn; |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 99 | using namespace ndn::security; |
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 { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 122 | po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), |
| 123 | vm); |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 124 | po::notify(vm); |
| 125 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 126 | catch (const std::exception& e) { |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 127 | std::cerr << "ERROR: " << e.what() << std::endl; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 128 | return 1; |
| 129 | } |
| 130 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 131 | if (vm.count("help") != 0) { |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 132 | std::cerr << description << std::endl; |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 133 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 136 | if (vm.count("cert-file") == 0) { |
| 137 | std::cerr << "cert_file must be specified" << std::endl; |
| 138 | std::cerr << description << std::endl; |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | if (vm.count("identity-default") != 0) { |
| 143 | isIdentityDefault = true; |
| 144 | isSystemDefault = false; |
| 145 | } |
| 146 | else if (vm.count("key-default") != 0) { |
| 147 | isKeyDefault = true; |
| 148 | isSystemDefault = false; |
| 149 | } |
| 150 | else if (vm.count("no-default") != 0) { |
| 151 | // noDefault = true; |
| 152 | isSystemDefault = false; |
| 153 | } |
| 154 | |
| 155 | shared_ptr<v1::IdentityCertificate> cert; |
| 156 | |
| 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) |
| 166 | throw HttpException("Request line is not correctly formatted"); |
| 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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [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 { |
| 185 | cert = getIdentityCertificate(certFileName); |
| 186 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 187 | |
| 188 | if (!static_cast<bool>(cert)) |
| 189 | return 1; |
| 190 | |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 191 | ndn::security::v1::KeyChain keyChain; |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 192 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 193 | if (isSystemDefault) { |
| 194 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 195 | Name keyName = cert->getPublicKeyName(); |
| 196 | Name identity = keyName.getSubName(0, keyName.size()-1); |
| 197 | keyChain.setDefaultIdentity(identity); |
| 198 | } |
| 199 | else if (isIdentityDefault) { |
| 200 | keyChain.addCertificateAsIdentityDefault(*cert); |
| 201 | } |
| 202 | else if (isKeyDefault) { |
| 203 | keyChain.addCertificateAsKeyDefault(*cert); |
| 204 | } |
| 205 | else { |
| 206 | keyChain.addCertificate(*cert); |
| 207 | } |
Yingdi Yu | b61f540 | 2014-02-26 17:46:11 -0800 | [diff] [blame] | 208 | |
| 209 | std::cerr << "OK: certificate with name [" |
| 210 | << cert->getName().toUri() |
| 211 | << "] has been successfully installed" |
| 212 | << std::endl; |
| 213 | |
| 214 | return 0; |
Yingdi Yu | e6bfab2 | 2014-02-06 16:01:19 -0800 | [diff] [blame] | 215 | } |
Yingdi Yu | 8d7468f | 2014-02-21 14:49:45 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | d7db8bf | 2015-01-04 15:31:02 -0800 | [diff] [blame] | 217 | #endif // NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP |