blob: 626dc627310edbb95b79e7b6856c5543594090ff [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yue6bfab22014-02-06 16:01:19 -080022 */
23
Yingdi Yu8d7468f2014-02-21 14:49:45 -080024#ifndef NDNSEC_CERT_INSTALL_HPP
25#define NDNSEC_CERT_INSTALL_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080026
Yingdi Yu8d7468f2014-02-21 14:49:45 -080027#include "ndnsec-util.hpp"
Yingdi Yue6bfab22014-02-06 16:01:19 -080028
Yingdi Yue6bfab22014-02-06 16:01:19 -080029
Yingdi Yub61f5402014-02-26 17:46:11 -080030class HttpException : public std::runtime_error
Yingdi Yue6bfab22014-02-06 16:01:19 -080031{
Yingdi Yub61f5402014-02-26 17:46:11 -080032public:
33 explicit
34 HttpException(const std::string& what)
35 : std::runtime_error(what)
Yingdi Yue6bfab22014-02-06 16:01:19 -080036 {
37 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080038};
39
Yingdi Yu8d7468f2014-02-21 14:49:45 -080040ndn::shared_ptr<ndn::IdentityCertificate>
Yingdi Yub61f5402014-02-26 17:46:11 -080041getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
Yingdi Yue6bfab22014-02-06 16:01:19 -080042{
43 using namespace boost::asio::ip;
Yingdi Yub61f5402014-02-26 17:46:11 -080044 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 Yue6bfab22014-02-06 16:01:19 -080050 {
51 throw HttpException("HTTP connection error");
52 }
Yingdi Yub61f5402014-02-26 17:46:11 -080053 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 Yue6bfab22014-02-06 16:01:19 -080059
Yingdi Yub61f5402014-02-26 17:46:11 -080060 std::string statusLine;
61 std::getline(requestStream, statusLine);
62 if (!static_cast<bool>(requestStream))
Yingdi Yue6bfab22014-02-06 16:01:19 -080063 {
64 throw HttpException("HTTP communication error");
65 }
66
Yingdi Yub61f5402014-02-26 17:46:11 -080067 std::stringstream responseStream(statusLine);
68 std::string httpVersion;
69 responseStream >> httpVersion;
70 unsigned int statusCode;
71 responseStream >> statusCode;
72 std::string statusMessage;
Yingdi Yue6bfab22014-02-06 16:01:19 -080073
Yingdi Yub61f5402014-02-26 17:46:11 -080074 std::getline(responseStream, statusMessage);
75 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/")
Yingdi Yue6bfab22014-02-06 16:01:19 -080076 {
77 throw HttpException("HTTP communication error");
78 }
Yingdi Yub61f5402014-02-26 17:46:11 -080079 if (statusCode != 200)
Yingdi Yue6bfab22014-02-06 16:01:19 -080080 {
81 throw HttpException("HTTP server error");
82 }
83 std::string header;
Yingdi Yub61f5402014-02-26 17:46:11 -080084 while (std::getline(requestStream, header) && header != "\r")
85 ;
Yingdi Yue6bfab22014-02-06 16:01:19 -080086
Yingdi Yu8d7468f2014-02-21 14:49:45 -080087 ndn::OBufferStream os;
Yingdi Yub61f5402014-02-26 17:46:11 -080088 {
89 using namespace CryptoPP;
90 FileSource ss2(requestStream, true, new Base64Decoder(new FileSink(os)));
91 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080092
Yingdi Yub61f5402014-02-26 17:46:11 -080093 ndn::shared_ptr<ndn::IdentityCertificate> identityCertificate =
94 ndn::make_shared<ndn::IdentityCertificate>();
Yingdi Yu8d7468f2014-02-21 14:49:45 -080095 identityCertificate->wireDecode(ndn::Block(os.buf()));
Yingdi Yue6bfab22014-02-06 16:01:19 -080096
97 return identityCertificate;
98}
99
Yingdi Yub61f5402014-02-26 17:46:11 -0800100int
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800101ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800102{
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800103 using namespace ndn;
104 namespace po = boost::program_options;
105
106 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -0800107 bool isSystemDefault = true;
108 bool isIdentityDefault = false;
109 bool isKeyDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800110
Yingdi Yub61f5402014-02-26 17:46:11 -0800111 po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options");
112 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -0800113 ("help,h", "produce help message")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800114 ("cert-file,f", po::value<std::string>(&certFileName), "file name of the ceritificate, - for stdin. "
Yingdi Yue6bfab22014-02-06 16:01:19 -0800115 "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 Yub61f5402014-02-26 17:46:11 -0800127 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
128 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800129 po::notify(vm);
130 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800131 catch (const std::exception& e)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800132 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800133 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800134 return 1;
135 }
136
Yingdi Yub61f5402014-02-26 17:46:11 -0800137 if (vm.count("help") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800138 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800139 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800140 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800141 }
142
Yingdi Yub61f5402014-02-26 17:46:11 -0800143 if (vm.count("cert-file") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800144 {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800145 std::cerr << "cert_file must be specified" << std::endl;
Yingdi Yub61f5402014-02-26 17:46:11 -0800146 std::cerr << description << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800147 return 1;
148 }
149
Yingdi Yub61f5402014-02-26 17:46:11 -0800150 if (vm.count("identity-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800151 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800152 isIdentityDefault = true;
153 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800154 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800155 else if (vm.count("key-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800156 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800157 isKeyDefault = true;
158 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800159 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800160 else if (vm.count("no-default") != 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800161 {
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700162 // noDefault = true;
Yingdi Yub61f5402014-02-26 17:46:11 -0800163 isSystemDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800164 }
165
Yingdi Yub61f5402014-02-26 17:46:11 -0800166 shared_ptr<IdentityCertificate> cert;
167
168 if (certFileName.find("http://") == 0)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800169 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800170 std::string host;
171 std::string port;
172 std::string path;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800173
Yingdi Yub61f5402014-02-26 17:46:11 -0800174 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 Yue6bfab22014-02-06 16:01:19 -0800183 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800184 port = certFileName.substr(posPort + 1, posSlash - posPort - 1);
185 host = certFileName.substr(pos, posPort - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800186 }
187 else
188 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800189 port = "80";
190 host = certFileName.substr(pos, posSlash - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800191 }
192
Yingdi Yub61f5402014-02-26 17:46:11 -0800193 path = certFileName.substr(posSlash, certFileName.size () - posSlash);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800194
Yingdi Yub61f5402014-02-26 17:46:11 -0800195 cert = getCertificateHttp(host, port, path);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800196 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800197 else
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800198 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800199 cert = getIdentityCertificate(certFileName);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800200 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800201
202 if (!static_cast<bool>(cert))
203 return 1;
204
205 KeyChain keyChain;
206
207 if (isSystemDefault)
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800208 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800209 keyChain.addCertificateAsIdentityDefault(*cert);
210 Name keyName = cert->getPublicKeyName();
211 Name identity = keyName.getSubName(0, keyName.size()-1);
212 keyChain.setDefaultIdentity(identity);
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800213 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800214 else if (isIdentityDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800215 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800216 keyChain.addCertificateAsIdentityDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800217 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800218 else if (isKeyDefault)
Yingdi Yue6bfab22014-02-06 16:01:19 -0800219 {
Yingdi Yub61f5402014-02-26 17:46:11 -0800220 keyChain.addCertificateAsKeyDefault(*cert);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800221 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800222 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 Yue6bfab22014-02-06 16:01:19 -0800233}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800234
235#endif //NDNSEC_CERT_INSTALL_HPP