/
cert-install.hpp
235 lines (201 loc) · 6.77 KB
/
cert-install.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2015 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*
* @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
*/
#ifndef NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP
#define NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP
#include "util.hpp"
class HttpException : public std::runtime_error
{
public:
explicit
HttpException(const std::string& what)
: std::runtime_error(what)
{
}
};
ndn::shared_ptr<ndn::IdentityCertificate>
getCertificateHttp(const std::string& host, const std::string& port, const std::string& path)
{
using namespace boost::asio::ip;
tcp::iostream requestStream;
requestStream.expires_from_now(boost::posix_time::milliseconds(3000));
requestStream.connect(host, port);
if (!static_cast<bool>(requestStream))
{
throw HttpException("HTTP connection error");
}
requestStream << "GET " << path << " HTTP/1.0\r\n";
requestStream << "Host: " << host << "\r\n";
requestStream << "Accept: */*\r\n";
requestStream << "Cache-Control: no-cache\r\n";
requestStream << "Connection: close\r\n\r\n";
requestStream.flush();
std::string statusLine;
std::getline(requestStream, statusLine);
if (!static_cast<bool>(requestStream))
{
throw HttpException("HTTP communication error");
}
std::stringstream responseStream(statusLine);
std::string httpVersion;
responseStream >> httpVersion;
unsigned int statusCode;
responseStream >> statusCode;
std::string statusMessage;
std::getline(responseStream, statusMessage);
if (!static_cast<bool>(requestStream) || httpVersion.substr(0, 5) != "HTTP/")
{
throw HttpException("HTTP communication error");
}
if (statusCode != 200)
{
throw HttpException("HTTP server error");
}
std::string header;
while (std::getline(requestStream, header) && header != "\r")
;
ndn::OBufferStream os;
{
using namespace CryptoPP;
FileSource ss2(requestStream, true, new Base64Decoder(new FileSink(os)));
}
ndn::shared_ptr<ndn::IdentityCertificate> identityCertificate =
ndn::make_shared<ndn::IdentityCertificate>();
identityCertificate->wireDecode(ndn::Block(os.buf()));
return identityCertificate;
}
int
ndnsec_cert_install(int argc, char** argv)
{
using namespace ndn;
namespace po = boost::program_options;
std::string certFileName;
bool isSystemDefault = true;
bool isIdentityDefault = false;
bool isKeyDefault = false;
po::options_description description("General Usage\n ndnsec cert-install [-h] [-I|K|N] cert-file\nGeneral options");
description.add_options()
("help,h", "produce help message")
("cert-file,f", po::value<std::string>(&certFileName), "file name of the ceritificate, - for stdin. "
"If starts with http://, will try to fetch "
"the certificate using HTTP GET request")
("identity-default,I", "optional, if specified, the certificate will be set as the default certificate of the identity")
("key-default,K", "optional, if specified, the certificate will be set as the default certificate of the key")
("no-default,N", "optional, if specified, the certificate will be simply installed")
;
po::positional_options_description p;
p.add("cert-file", 1);
po::variables_map vm;
try
{
po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
vm);
po::notify(vm);
}
catch (const std::exception& e)
{
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
if (vm.count("help") != 0)
{
std::cerr << description << std::endl;
return 0;
}
if (vm.count("cert-file") == 0)
{
std::cerr << "cert_file must be specified" << std::endl;
std::cerr << description << std::endl;
return 1;
}
if (vm.count("identity-default") != 0)
{
isIdentityDefault = true;
isSystemDefault = false;
}
else if (vm.count("key-default") != 0)
{
isKeyDefault = true;
isSystemDefault = false;
}
else if (vm.count("no-default") != 0)
{
// noDefault = true;
isSystemDefault = false;
}
shared_ptr<IdentityCertificate> cert;
if (certFileName.find("http://") == 0)
{
std::string host;
std::string port;
std::string path;
size_t pos = 7; // offset of "http://"
size_t posSlash = certFileName.find("/", pos);
if (posSlash == std::string::npos)
throw HttpException("Request line is not correctly formatted");
size_t posPort = certFileName.find(":", pos);
if (posPort != std::string::npos && posPort < posSlash) // port is specified
{
port = certFileName.substr(posPort + 1, posSlash - posPort - 1);
host = certFileName.substr(pos, posPort - pos);
}
else
{
port = "80";
host = certFileName.substr(pos, posSlash - pos);
}
path = certFileName.substr(posSlash, certFileName.size () - posSlash);
cert = getCertificateHttp(host, port, path);
}
else
{
cert = getIdentityCertificate(certFileName);
}
if (!static_cast<bool>(cert))
return 1;
KeyChain keyChain;
if (isSystemDefault)
{
keyChain.addCertificateAsIdentityDefault(*cert);
Name keyName = cert->getPublicKeyName();
Name identity = keyName.getSubName(0, keyName.size()-1);
keyChain.setDefaultIdentity(identity);
}
else if (isIdentityDefault)
{
keyChain.addCertificateAsIdentityDefault(*cert);
}
else if (isKeyDefault)
{
keyChain.addCertificateAsKeyDefault(*cert);
}
else
{
keyChain.addCertificate(*cert);
}
std::cerr << "OK: certificate with name ["
<< cert->getName().toUri()
<< "] has been successfully installed"
<< std::endl;
return 0;
}
#endif // NDN_TOOLS_NDNSEC_CERT_INSTALL_HPP