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