blob: f23eb7a40fda45256468109ba7b5ba30a8242458 [file] [log] [blame]
Yingdi Yue6bfab22014-02-06 16:01:19 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
Yingdi Yue6bfab22014-02-06 16:01:19 -08004 * BSD license, See the LICENSE file for more information
Yingdi Yue6bfab22014-02-06 16:01:19 -08005 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
6 */
7
Yingdi Yu8d7468f2014-02-21 14:49:45 -08008#ifndef NDNSEC_CERT_INSTALL_HPP
9#define NDNSEC_CERT_INSTALL_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080010
Yingdi Yu8d7468f2014-02-21 14:49:45 -080011#include "ndnsec-util.hpp"
Yingdi Yue6bfab22014-02-06 16:01:19 -080012
Yingdi Yue6bfab22014-02-06 16:01:19 -080013
Yingdi Yub61f5402014-02-26 17:46:11 -080014class HttpException : public std::runtime_error
Yingdi Yue6bfab22014-02-06 16:01:19 -080015{
Yingdi Yub61f5402014-02-26 17:46:11 -080016public:
17 explicit
18 HttpException(const std::string& what)
19 : std::runtime_error(what)
Yingdi Yue6bfab22014-02-06 16:01:19 -080020 {
21 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080022};
23
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024ndn::shared_ptr<ndn::IdentityCertificate>
Yingdi Yub61f5402014-02-26 17:46:11 -080025getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
Yingdi Yue6bfab22014-02-06 16:01:19 -080026{
27 using namespace boost::asio::ip;
Yingdi Yub61f5402014-02-26 17:46:11 -080028 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 Yue6bfab22014-02-06 16:01:19 -080034 {
35 throw HttpException("HTTP connection error");
36 }
Yingdi Yub61f5402014-02-26 17:46:11 -080037 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 Yue6bfab22014-02-06 16:01:19 -080043
Yingdi Yub61f5402014-02-26 17:46:11 -080044 std::string statusLine;
45 std::getline(requestStream, statusLine);
46 if (!static_cast<bool>(requestStream))
Yingdi Yue6bfab22014-02-06 16:01:19 -080047 {
48 throw HttpException("HTTP communication error");
49 }
50
Yingdi Yub61f5402014-02-26 17:46:11 -080051 std::stringstream responseStream(statusLine);
52 std::string httpVersion;
53 responseStream >> httpVersion;
54 unsigned int statusCode;
55 responseStream >> statusCode;
56 std::string statusMessage;
Yingdi Yue6bfab22014-02-06 16:01:19 -080057
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::getline(responseStream, statusMessage);
59 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/")
Yingdi Yue6bfab22014-02-06 16:01:19 -080060 {
61 throw HttpException("HTTP communication error");
62 }
Yingdi Yub61f5402014-02-26 17:46:11 -080063 if (statusCode != 200)
Yingdi Yue6bfab22014-02-06 16:01:19 -080064 {
65 throw HttpException("HTTP server error");
66 }
67 std::string header;
Yingdi Yub61f5402014-02-26 17:46:11 -080068 while (std::getline(requestStream, header) && header != "\r")
69 ;
Yingdi Yue6bfab22014-02-06 16:01:19 -080070
Yingdi Yu8d7468f2014-02-21 14:49:45 -080071 ndn::OBufferStream os;
Yingdi Yub61f5402014-02-26 17:46:11 -080072 {
73 using namespace CryptoPP;
74 FileSource ss2(requestStream, true, new Base64Decoder(new FileSink(os)));
75 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080076
Yingdi Yub61f5402014-02-26 17:46:11 -080077 ndn::shared_ptr<ndn::IdentityCertificate> identityCertificate =
78 ndn::make_shared<ndn::IdentityCertificate>();
Yingdi Yu8d7468f2014-02-21 14:49:45 -080079 identityCertificate->wireDecode(ndn::Block(os.buf()));
Yingdi Yue6bfab22014-02-06 16:01:19 -080080
81 return identityCertificate;
82}
83
Yingdi Yub61f5402014-02-26 17:46:11 -080084int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080085ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080086{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087 using namespace ndn;
88 namespace po = boost::program_options;
89
90 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -080091 bool isSystemDefault = true;
92 bool isIdentityDefault = false;
93 bool isKeyDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -080094
Yingdi Yub61f5402014-02-26 17:46:11 -080095 po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options");
96 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -080097 ("help,h", "produce help message")
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 ("cert-file,f", po::value<std::string>(&certFileName), "file name of the ceritificate, - for stdin. "
Yingdi Yue6bfab22014-02-06 16:01:19 -080099 "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 Yub61f5402014-02-26 17:46:11 -0800111 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
112 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800113 po::notify(vm);
114 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800115 catch (const std::exception& e)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800116 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800117 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800118 return 1;
119 }
120
Yingdi Yub61f5402014-02-26 17:46:11 -0800121 if (vm.count("help") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800122 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800123 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800124 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800125 }
126
Yingdi Yub61f5402014-02-26 17:46:11 -0800127 if (vm.count("cert-file") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800128 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800129 std::cerr << "cert_file must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800130 std::cerr << description << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800131 return 1;
132 }
133
Yingdi Yub61f5402014-02-26 17:46:11 -0800134 if (vm.count("identity-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800135 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800136 isIdentityDefault = true;
137 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800138 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800139 else if (vm.count("key-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800140 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800141 isKeyDefault = true;
142 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800143 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800144 else if (vm.count("no-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800145 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700146 // noDefault = true;
Yingdi Yub61f5402014-02-26 17:46:11 -0800147 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800148 }
149
Yingdi Yub61f5402014-02-26 17:46:11 -0800150 shared_ptr<IdentityCertificate> cert;
151
152 if (certFileName.find("http://") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800153 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800154 std::string host;
155 std::string port;
156 std::string path;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800157
Yingdi Yub61f5402014-02-26 17:46:11 -0800158 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 Yue6bfab22014-02-06 16:01:19 -0800167 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800168 port = certFileName.substr(posPort + 1, posSlash - posPort - 1);
169 host = certFileName.substr(pos, posPort - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800170 }
171 else
172 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800173 port = "80";
174 host = certFileName.substr(pos, posSlash - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800175 }
176
Yingdi Yub61f5402014-02-26 17:46:11 -0800177 path = certFileName.substr(posSlash, certFileName.size () - posSlash);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800178
Yingdi Yub61f5402014-02-26 17:46:11 -0800179 cert = getCertificateHttp(host, port, path);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800180 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800181 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800182 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800183 cert = getIdentityCertificate(certFileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800184 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800185
186 if (!static_cast<bool>(cert))
187 return 1;
188
189 KeyChain keyChain;
190
191 if (isSystemDefault)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800192 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800193 keyChain.addCertificateAsIdentityDefault(*cert);
194 Name keyName = cert->getPublicKeyName();
195 Name identity = keyName.getSubName(0, keyName.size()-1);
196 keyChain.setDefaultIdentity(identity);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800197 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800198 else if (isIdentityDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800199 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800200 keyChain.addCertificateAsIdentityDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800201 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800202 else if (isKeyDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800203 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800204 keyChain.addCertificateAsKeyDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800205 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800206 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 Yue6bfab22014-02-06 16:01:19 -0800217}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800218
219#endif //NDNSEC_CERT_INSTALL_HPP