13
13
14
14
namespace rtc ::impl {
15
15
16
+ static const string PemBeginCertificateTag = " -----BEGIN CERTIFICATE-----" ;
17
+
16
18
VerifiedTlsTransport::VerifiedTlsTransport (
17
19
variant<shared_ptr<TcpTransport>, shared_ptr<HttpProxyTransport>> lower, string host,
18
- certificate_ptr certificate, state_callback callback)
20
+ certificate_ptr certificate, state_callback callback, [[maybe_unused]] optional<string> cacert )
19
21
: TlsTransport(std::move(lower), std::move(host), std::move(certificate), std::move(callback)) {
20
22
21
23
PLOG_DEBUG << " Setting up TLS certificate verification" ;
@@ -24,13 +26,36 @@ VerifiedTlsTransport::VerifiedTlsTransport(
24
26
gnutls_session_set_verify_cert (mSession , mHost ->c_str (), 0 );
25
27
#elif USE_MBEDTLS
26
28
mbedtls_ssl_conf_authmode (&mConf , MBEDTLS_SSL_VERIFY_REQUIRED);
29
+ mbedtls_x509_crt_init (&mCaCert );
30
+ try {
31
+ if (cacert) {
32
+ if (cacert->find (PemBeginCertificateTag) == string::npos) {
33
+ // *cacert is a file path
34
+ mbedtls::check (mbedtls_x509_crt_parse_file (&mCaCert , cacert->c_str ()));
35
+ } else {
36
+ // *cacert is a PEM content
37
+ mbedtls::check (mbedtls_x509_crt_parse (
38
+ &mCaCert , reinterpret_cast <const unsigned char *>(cacert->c_str ()),
39
+ cacert->size ()));
40
+ }
41
+ mbedtls_ssl_conf_ca_chain (&mConf , &mCaCert , NULL );
42
+ }
43
+ } catch (...) {
44
+ mbedtls_x509_crt_free (&mCaCert );
45
+ throw ;
46
+ }
27
47
#else
28
48
SSL_set_verify (mSsl , SSL_VERIFY_PEER, NULL );
29
49
SSL_set_verify_depth (mSsl , 4 );
30
50
#endif
31
51
}
32
52
33
- VerifiedTlsTransport::~VerifiedTlsTransport () { stop (); }
53
+ VerifiedTlsTransport::~VerifiedTlsTransport () {
54
+ stop ();
55
+ #if USE_MBEDTLS
56
+ mbedtls_x509_crt_free (&mCaCert );
57
+ #endif
58
+ }
34
59
35
60
} // namespace rtc::impl
36
61
0 commit comments