blob: 2a0b46f53e8cd8ba60fc40dff6f65e0d9aac34e9 [file] [log] [blame]
Yingdi Yue6bfab22014-02-06 16:01:19 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
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 Yue6bfab22014-02-06 16:01:19 -080013 */
14
Yingdi Yu8d7468f2014-02-21 14:49:45 -080015#ifndef NDNSEC_CERT_INSTALL_HPP
16#define NDNSEC_CERT_INSTALL_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080017
Yingdi Yu8d7468f2014-02-21 14:49:45 -080018#include "ndnsec-util.hpp"
Yingdi Yue6bfab22014-02-06 16:01:19 -080019
Yingdi Yue6bfab22014-02-06 16:01:19 -080020
Yingdi Yub61f5402014-02-26 17:46:11 -080021class HttpException : public std::runtime_error
Yingdi Yue6bfab22014-02-06 16:01:19 -080022{
Yingdi Yub61f5402014-02-26 17:46:11 -080023public:
24 explicit
25 HttpException(const std::string& what)
26 : std::runtime_error(what)
Yingdi Yue6bfab22014-02-06 16:01:19 -080027 {
28 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080029};
30
Yingdi Yu8d7468f2014-02-21 14:49:45 -080031ndn::shared_ptr<ndn::IdentityCertificate>
Yingdi Yub61f5402014-02-26 17:46:11 -080032getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
Yingdi Yue6bfab22014-02-06 16:01:19 -080033{
34 using namespace boost::asio::ip;
Yingdi Yub61f5402014-02-26 17:46:11 -080035 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 Yue6bfab22014-02-06 16:01:19 -080041 {
42 throw HttpException("HTTP connection error");
43 }
Yingdi Yub61f5402014-02-26 17:46:11 -080044 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 Yue6bfab22014-02-06 16:01:19 -080050
Yingdi Yub61f5402014-02-26 17:46:11 -080051 std::string statusLine;
52 std::getline(requestStream, statusLine);
53 if (!static_cast<bool>(requestStream))
Yingdi Yue6bfab22014-02-06 16:01:19 -080054 {
55 throw HttpException("HTTP communication error");
56 }
57
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::stringstream responseStream(statusLine);
59 std::string httpVersion;
60 responseStream >> httpVersion;
61 unsigned int statusCode;
62 responseStream >> statusCode;
63 std::string statusMessage;
Yingdi Yue6bfab22014-02-06 16:01:19 -080064
Yingdi Yub61f5402014-02-26 17:46:11 -080065 std::getline(responseStream, statusMessage);
66 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/")
Yingdi Yue6bfab22014-02-06 16:01:19 -080067 {
68 throw HttpException("HTTP communication error");
69 }
Yingdi Yub61f5402014-02-26 17:46:11 -080070 if (statusCode != 200)
Yingdi Yue6bfab22014-02-06 16:01:19 -080071 {
72 throw HttpException("HTTP server error");
73 }
74 std::string header;
Yingdi Yub61f5402014-02-26 17:46:11 -080075 while (std::getline(requestStream, header) && header != "\r")
76 ;
Yingdi Yue6bfab22014-02-06 16:01:19 -080077
Yingdi Yu8d7468f2014-02-21 14:49:45 -080078 ndn::OBufferStream os;
Yingdi Yub61f5402014-02-26 17:46:11 -080079 {
80 using namespace CryptoPP;
81 FileSource ss2(requestStream, true, new Base64Decoder(new FileSink(os)));
82 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080083
Yingdi Yub61f5402014-02-26 17:46:11 -080084 ndn::shared_ptr<ndn::IdentityCertificate> identityCertificate =
85 ndn::make_shared<ndn::IdentityCertificate>();
Yingdi Yu8d7468f2014-02-21 14:49:45 -080086 identityCertificate->wireDecode(ndn::Block(os.buf()));
Yingdi Yue6bfab22014-02-06 16:01:19 -080087
88 return identityCertificate;
89}
90
Yingdi Yub61f5402014-02-26 17:46:11 -080091int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080092ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080093{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080094 using namespace ndn;
95 namespace po = boost::program_options;
96
97 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -080098 bool isSystemDefault = true;
99 bool isIdentityDefault = false;
100 bool isKeyDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800101
Yingdi Yub61f5402014-02-26 17:46:11 -0800102 po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options");
103 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -0800104 ("help,h", "produce help message")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800105 ("cert-file,f", po::value<std::string>(&certFileName), "file name of the ceritificate, - for stdin. "
Yingdi Yue6bfab22014-02-06 16:01:19 -0800106 "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 Yub61f5402014-02-26 17:46:11 -0800118 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
119 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800120 po::notify(vm);
121 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800122 catch (const std::exception& e)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800123 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800124 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800125 return 1;
126 }
127
Yingdi Yub61f5402014-02-26 17:46:11 -0800128 if (vm.count("help") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800129 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800130 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800131 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800132 }
133
Yingdi Yub61f5402014-02-26 17:46:11 -0800134 if (vm.count("cert-file") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800135 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800136 std::cerr << "cert_file must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800137 std::cerr << description << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800138 return 1;
139 }
140
Yingdi Yub61f5402014-02-26 17:46:11 -0800141 if (vm.count("identity-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800142 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800143 isIdentityDefault = true;
144 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800145 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800146 else if (vm.count("key-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800147 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800148 isKeyDefault = true;
149 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800150 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800151 else if (vm.count("no-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800152 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700153 // noDefault = true;
Yingdi Yub61f5402014-02-26 17:46:11 -0800154 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800155 }
156
Yingdi Yub61f5402014-02-26 17:46:11 -0800157 shared_ptr<IdentityCertificate> cert;
158
159 if (certFileName.find("http://") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800160 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800161 std::string host;
162 std::string port;
163 std::string path;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800164
Yingdi Yub61f5402014-02-26 17:46:11 -0800165 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 Yue6bfab22014-02-06 16:01:19 -0800174 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800175 port = certFileName.substr(posPort + 1, posSlash - posPort - 1);
176 host = certFileName.substr(pos, posPort - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800177 }
178 else
179 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800180 port = "80";
181 host = certFileName.substr(pos, posSlash - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800182 }
183
Yingdi Yub61f5402014-02-26 17:46:11 -0800184 path = certFileName.substr(posSlash, certFileName.size () - posSlash);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800185
Yingdi Yub61f5402014-02-26 17:46:11 -0800186 cert = getCertificateHttp(host, port, path);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800187 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800188 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800189 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800190 cert = getIdentityCertificate(certFileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800191 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800192
193 if (!static_cast<bool>(cert))
194 return 1;
195
196 KeyChain keyChain;
197
198 if (isSystemDefault)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800199 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800200 keyChain.addCertificateAsIdentityDefault(*cert);
201 Name keyName = cert->getPublicKeyName();
202 Name identity = keyName.getSubName(0, keyName.size()-1);
203 keyChain.setDefaultIdentity(identity);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800204 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800205 else if (isIdentityDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800206 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800207 keyChain.addCertificateAsIdentityDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800208 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800209 else if (isKeyDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800210 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800211 keyChain.addCertificateAsKeyDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800212 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800213 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 Yue6bfab22014-02-06 16:01:19 -0800224}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800225
226#endif //NDNSEC_CERT_INSTALL_HPP