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