FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/http/Ssl.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
http
/
Ssl.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
261 lines (232 loc) · 8.22 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
http
/
Ssl.cpp
Copy path
File metadata and controls
261 lines (232 loc) · 8.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Ssl.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/http/Ssl.hpp
>
#
include
<
core/Log.hpp
>
#
ifdef
_WIN32
#
include
<
windows.h
>
#
include
<
wincrypt.h
>
#
include
<
cryptuiapi.h
>
#
include
<
openssl/x509.h
>
#
endif
#
ifdef
__APPLE__
#
include
<
core/http/Keychain.hpp
>
#
include
<
openssl/x509.h
>
#
endif
namespace
rstudio
{
namespace
core
{
namespace
http
{
namespace
ssl
{
namespace
{
#
if
defined(_WIN32) || defined(__APPLE__)
class
ICertStore
{
public:
virtual
std::vector<
X509
*>
getCertificates
()
const
= 0;
virtual
~ICertStore
() {}
};
#
endif
#
ifdef
_WIN32
class
WindowsCertificateStore
:
public
ICertStore
{
public:
WindowsCertificateStore
()
{
//
load certificates from important stores
LPCSTR
stores[] = {
"
ROOT
"
,
"
CA
"
};
for
(
const
LPCSTR
& store : stores)
{
HCERTSTORE
hStore =
CertOpenSystemStore
(
NULL
, store);
if
(!hStore)
{
LOG_ERROR_MESSAGE
(
"
Could not open certificate store
"
);
return
;
}
PCCERT_CONTEXT
pContext =
nullptr
;
while
(pContext =
CertEnumCertificatesInStore
(hStore, pContext))
{
//
convert the certificate returned from the Windows store into a
//
format that OpenSSL can understand
const
BYTE
* certPtr = pContext->
pbCertEncoded
;
X509
* x509 =
d2i_X509
(
nullptr
, &certPtr, pContext->
cbCertEncoded
);
if
(x509)
{
certificates.
push_back
(x509);
}
else
{
boost::system::error_code ec =
boost::system::error_code
(
static_cast
<
int
>(::
ERR_get_error
()),
boost::asio::error::get_ssl_category
());
Error
error
(ec,
ERROR_LOCATION
);
error.
addProperty
(
"
description
"
,
"
Could not create OpenSSL certificate object from Windows certificate data
"
);
LOG_DEBUG_MESSAGE
(error.
asString
());
}
}
CertCloseStore
(hStore,
0
);
}
}
//
note to caller - the certificate data (X509 pointers) here must not be freed as the data
//
must persist for the lifetime of the process
std::vector<
X509
*>
getCertificates
()
const
{
return
certificates;
}
private:
//
certificate pointers - these are intentionally leaked
//
as they need to be available for the entire run of the program
std::vector<
X509
*> certificates;
};
static
const
ICertStore&
getCertificateStore
()
{
//
Meyer's singleton - guarantees this is thread safe
//
and will be initialized exactly once by the first caller
static
WindowsCertificateStore instance;
return
instance;
}
#
endif
#
ifdef
__APPLE__
class
Keychain
:
public
ICertStore
{
public:
Keychain
()
{
//
load all certs from the keychain
std::vector<KeychainCertificateData> certs =
getKeychainCertificates
();
for
(
const
auto
& cert : certs)
{
//
convert the raw bytes from the keychain into a format that OpenSSL can understand
const
unsigned
char
* bytePtr = cert.
data
.
get
();
X509
* x509 =
d2i_X509
(
nullptr
, &bytePtr, cert.
size
);
if
(x509)
{
certificates.
push_back
(x509);
}
else
{
boost::system::error_code ec =
boost::system::error_code
(
static_cast
<
int
>(::
ERR_get_error
()),
boost::asio::error::get_ssl_category
());
Error
error
(ec,
ERROR_LOCATION
);
error.
addProperty
(
"
description
"
,
"
Could not create OpenSSL certificate object from Keychain certificate data
"
);
LOG_DEBUG_MESSAGE
(error.
asString
());
}
}
}
//
note to caller - the certificate data (X509 pointers) here must not be freed as the data
//
must persist for the lifetime of the process
std::vector<
X509
*>
getCertificates
()
const
{
return
certificates;
}
private:
//
certificate pointers - these are intentionally leaked
//
as they need to be available for the entire run of the program
std::vector<
X509
*> certificates;
};
static
const
ICertStore&
getKeychain
()
{
//
Meyer's singleton - guarantees this is thread safe
//
and will be initialized exactly once by the first caller
static
Keychain instance;
return
instance;
}
#
endif
}
//
namespace
void
initializeSslContext
(boost::asio::ssl::context* pContext,
bool
verify,
const
std::string& certificateAuthority)
{
if
(verify)
{
boost::system::error_code ec;
pContext->
set_verify_mode
(boost::asio::ssl::context::verify_peer, ec);
if
(ec)
{
LOG_ERROR
(
Error
(ec,
"
Could not enable certificate verification on SSL context
"
,
ERROR_LOCATION
));
ec.
clear
();
}
pContext->
set_default_verify_paths
(ec);
if
(ec)
{
LOG_ERROR
(
Error
(ec,
"
Could not set default certificate verification paths on SSL context
"
,
ERROR_LOCATION
));
ec.
clear
();
}
if
(!certificateAuthority.
empty
())
{
boost::asio::const_buffer
buff
(certificateAuthority.
data
(), certificateAuthority.
size
());
pContext->
add_certificate_authority
(buff, ec);
if
(ec)
{
LOG_ERROR
(
Error
(ec,
"
Could not add certificate authority to SSL context - is it valid?
"
,
ERROR_LOCATION
));
ec.
clear
();
}
}
#
ifdef
_WIN32
//
on Windows, OpenSSL does not support loading certificates from the Windows certificate store
//
because of this, each time we need to verify certificates, we initialize
//
all certificates individually with OpenSSL
const
ICertStore& certStore =
getCertificateStore
();
#
endif
#
ifdef
__APPLE__
//
on OSX, OpenSSL does not support loading certificates from the Keychain
//
because of this, each time we need to verify certificates, we initialize
//
all certificates individually with OpenSSL, similar to what is done above for Windows
const
ICertStore& certStore =
getKeychain
();
#
endif
#
if
defined(__APPLE__) || defined(_WIN32)
for
(
const
auto
& cert : certStore.
getCertificates
())
{
if
(
X509_STORE
* store =
SSL_CTX_get_cert_store
(pContext->
native_handle
()))
{
if
(::
X509_STORE_add_cert
(store, cert) !=
1
)
{
char
* subjectName =
X509_NAME_oneline
(
X509_get_subject_name
(cert),
nullptr
,
0
);
std::string
subjectNameStr
(subjectName);
OPENSSL_free
(subjectName);
ec =
boost::system::error_code
(
static_cast
<
int
>(::
ERR_get_error
()),
boost::asio::error::get_ssl_category
());
Error
error
(ec,
ERROR_LOCATION
);
error.
addProperty
(
"
description
"
,
"
Could not add certificate to OpenSSL cert store:
"
+ subjectNameStr);
LOG_ERROR
(error);
}
}
}
#
endif
}
else
{
boost::system::error_code ec;
pContext->
set_verify_mode
(boost::asio::ssl::context::verify_none, ec);
if
(ec)
LOG_ERROR
(
Error
(ec,
"
Could not disable certificate verification on SSL context
"
,
ERROR_LOCATION
));
}
}
void
initializeSslStream
(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>* pSslStream,
const
std::string& host)
{
//
TLS v1.3 requires that the SNI be set, so set the SNI.
if
(!
SSL_set_tlsext_host_name
(pSslStream->
native_handle
(), host.
c_str
()))
{
boost::system::error_code ec{
static_cast
<
int
>(::
ERR_get_error
()),
boost::asio::error::get_ssl_category
() };
LOG_ERROR
(
Error
(ec,
ERROR_LOCATION
));
}
}
}
//
namespace ssl
}
//
namespace http
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL