blob: a1416ff1f1eaf828078b8bbc163de99e294d5a74 [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 Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080024#ifndef NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP
25#define NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP
Yingdi Yue6bfab22014-02-06 16:01:19 -080026
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -080027#include "util.hpp"
Yingdi Yue6bfab22014-02-06 16:01:19 -080028
Yingdi Yub61f5402014-02-26 17:46:11 -080029class HttpException : public std::runtime_error
Yingdi Yue6bfab22014-02-06 16:01:19 -080030{
Yingdi Yub61f5402014-02-26 17:46:11 -080031public:
32 explicit
33 HttpException(const std::string& what)
34 : std::runtime_error(what)
Yingdi Yue6bfab22014-02-06 16:01:19 -080035 {
36 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080037};
38
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070039ndn::shared_ptr<ndn::security::v1::IdentityCertificate>
Yingdi Yub61f5402014-02-26 17:46:11 -080040getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
Yingdi Yue6bfab22014-02-06 16:01:19 -080041{
42 using namespace boost::asio::ip;
Yingdi Yub61f5402014-02-26 17:46:11 -080043 tcp::iostream requestStream;
44
45 requestStream.expires_from_now(boost::posix_time::milliseconds(3000));
46
47 requestStream.connect(host, port);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070048 if (!static_cast<bool>(requestStream)) {
49 throw HttpException("HTTP connection error");
50 }
Yingdi Yub61f5402014-02-26 17:46:11 -080051 requestStream << "GET " << path << " HTTP/1.0\r\n";
52 requestStream << "Host: " << host << "\r\n";
53 requestStream << "Accept: */*\r\n";
54 requestStream << "Cache-Control: no-cache\r\n";
55 requestStream << "Connection: close\r\n\r\n";
56 requestStream.flush();
Yingdi Yue6bfab22014-02-06 16:01:19 -080057
Yingdi Yub61f5402014-02-26 17:46:11 -080058 std::string statusLine;
59 std::getline(requestStream, statusLine);
60 if (!static_cast<bool>(requestStream))
Yingdi Yue6bfab22014-02-06 16:01:19 -080061 {
62 throw HttpException("HTTP communication error");
63 }
64
Yingdi Yub61f5402014-02-26 17:46:11 -080065 std::stringstream responseStream(statusLine);
66 std::string httpVersion;
67 responseStream >> httpVersion;
68 unsigned int statusCode;
69 responseStream >> statusCode;
70 std::string statusMessage;
Yingdi Yue6bfab22014-02-06 16:01:19 -080071
Yingdi Yub61f5402014-02-26 17:46:11 -080072 std::getline(responseStream, statusMessage);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070073 if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/") {
74 throw HttpException("HTTP communication error");
75 }
76 if (statusCode != 200) {
77 throw HttpException("HTTP server error");
78 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080079 std::string header;
Yingdi Yub61f5402014-02-26 17:46:11 -080080 while (std::getline(requestStream, header) && header != "\r")
81 ;
Yingdi Yue6bfab22014-02-06 16:01:19 -080082
Yingdi Yu8d7468f2014-02-21 14:49:45 -080083 ndn::OBufferStream os;
Yingdi Yub61f5402014-02-26 17:46:11 -080084 {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070085 using namespace ndn::security::transform;
86 streamSource(requestStream) >> base64Decode(true) >> streamSink(os);
Yingdi Yub61f5402014-02-26 17:46:11 -080087 }
Yingdi Yue6bfab22014-02-06 16:01:19 -080088
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070089 auto identityCertificate = std::make_shared<ndn::security::v1::IdentityCertificate>();
Yingdi Yu8d7468f2014-02-21 14:49:45 -080090 identityCertificate->wireDecode(ndn::Block(os.buf()));
Yingdi Yue6bfab22014-02-06 16:01:19 -080091
92 return identityCertificate;
93}
94
Yingdi Yub61f5402014-02-26 17:46:11 -080095int
Yingdi Yu8d7468f2014-02-21 14:49:45 -080096ndnsec_cert_install(int argc, char** argv)
Yingdi Yue6bfab22014-02-06 16:01:19 -080097{
Yingdi Yu8d7468f2014-02-21 14:49:45 -080098 using namespace ndn;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070099 using namespace ndn::security;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800100 namespace po = boost::program_options;
101
102 std::string certFileName;
Yingdi Yub61f5402014-02-26 17:46:11 -0800103 bool isSystemDefault = true;
104 bool isIdentityDefault = false;
105 bool isKeyDefault = false;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800106
Yingdi Yub61f5402014-02-26 17:46:11 -0800107 po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options");
108 description.add_options()
Yingdi Yue6bfab22014-02-06 16:01:19 -0800109 ("help,h", "produce help message")
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800110 ("cert-file,f", po::value<std::string>(&certFileName), "file name of the ceritificate, - for stdin. "
Yingdi Yue6bfab22014-02-06 16:01:19 -0800111 "If starts with http://, will try to fetch "
112 "the certificate using HTTP GET request")
113 ("identity-default,I", "optional, if specified, the certificate will be set as the default certificate of the identity")
114 ("key-default,K", "optional, if specified, the certificate will be set as the default certificate of the key")
115 ("no-default,N", "optional, if specified, the certificate will be simply installed")
116 ;
117 po::positional_options_description p;
118 p.add("cert-file", 1);
119
120 po::variables_map vm;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700121 try {
Yingdi Yub61f5402014-02-26 17:46:11 -0800122 po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
123 vm);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800124 po::notify(vm);
125 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700126 catch (const std::exception& e) {
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800127 std::cerr << "ERROR: " << e.what() << std::endl;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800128 return 1;
129 }
130
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700131 if (vm.count("help") != 0) {
Yingdi Yub61f5402014-02-26 17:46:11 -0800132 std::cerr << description << std::endl;
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800133 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800134 }
135
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700136 if (vm.count("cert-file") == 0) {
137 std::cerr << "cert_file must be specified" << std::endl;
138 std::cerr << description << std::endl;
139 return 1;
140 }
141
142 if (vm.count("identity-default") != 0) {
143 isIdentityDefault = true;
144 isSystemDefault = false;
145 }
146 else if (vm.count("key-default") != 0) {
147 isKeyDefault = true;
148 isSystemDefault = false;
149 }
150 else if (vm.count("no-default") != 0) {
151 // noDefault = true;
152 isSystemDefault = false;
153 }
154
155 shared_ptr<v1::IdentityCertificate> cert;
156
157 if (certFileName.find("http://") == 0) {
158 std::string host;
159 std::string port;
160 std::string path;
161
162 size_t pos = 7; // offset of "http://"
163 size_t posSlash = certFileName.find("/", pos);
164
165 if (posSlash == std::string::npos)
166 throw HttpException("Request line is not correctly formatted");
167
168 size_t posPort = certFileName.find(":", pos);
169
170 if (posPort != std::string::npos && posPort < posSlash) {
171 // port is specified
172 port = certFileName.substr(posPort + 1, posSlash - posPort - 1);
173 host = certFileName.substr(pos, posPort - pos);
174 }
175 else {
176 port = "80";
177 host = certFileName.substr(pos, posSlash - pos);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800178 }
179
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700180 path = certFileName.substr(posSlash, certFileName.size () - posSlash);
Yingdi Yue6bfab22014-02-06 16:01:19 -0800181
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700182 cert = getCertificateHttp(host, port, path);
183 }
184 else {
185 cert = getIdentityCertificate(certFileName);
186 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800187
188 if (!static_cast<bool>(cert))
189 return 1;
190
191 KeyChain keyChain;
192
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700193 if (isSystemDefault) {
194 keyChain.addCertificateAsIdentityDefault(*cert);
195 Name keyName = cert->getPublicKeyName();
196 Name identity = keyName.getSubName(0, keyName.size()-1);
197 keyChain.setDefaultIdentity(identity);
198 }
199 else if (isIdentityDefault) {
200 keyChain.addCertificateAsIdentityDefault(*cert);
201 }
202 else if (isKeyDefault) {
203 keyChain.addCertificateAsKeyDefault(*cert);
204 }
205 else {
206 keyChain.addCertificate(*cert);
207 }
Yingdi Yub61f5402014-02-26 17:46:11 -0800208
209 std::cerr << "OK: certificate with name ["
210 << cert->getName().toUri()
211 << "] has been successfully installed"
212 << std::endl;
213
214 return 0;
Yingdi Yue6bfab22014-02-06 16:01:19 -0800215}
Yingdi Yu8d7468f2014-02-21 14:49:45 -0800216
Alexander Afanasyevd7db8bf2015-01-04 15:31:02 -0800217#endif // NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP