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